PracticeVideosBlogFlashcards

3.3 Many-to-Many Relationships & Junction Objects

Key Takeaways

  • Many-to-Many relationships are modeled using a custom junction object containing two separate Master-Detail relationships pointing to two parent objects.

  • The Primary Master is established by the first master-detail relationship created on the junction object, controlling the junction record's visual styling, tab colors, and ownership.

  • Deleting or converting the primary master-detail relationship field makes the secondary relationship primary. Deleting an individual master record instead cascade-deletes its junction records.

  • Junction access depends on both parent records, both relationship Sharing Settings, and the user's junction-object permissions; Read/Write on both parents is required when both relationships use that setting.

  • Deleting either parent master record causes the junction record to be cascade-deleted automatically into the Recycle Bin.

Last updated: October 2026

Many-to-Many Relationships & Junction Objects

Important

When business data requires that multiple records of Object A associate with multiple records of Object B, standard one-to-many relationships are insufficient. Salesforce models many-to-many relationships through a custom Junction Object containing two Master-Detail relationships.

In relational database modeling, a many-to-many relationship cannot be achieved by placing a single foreign key on either parent table. For example, consider an enterprise recruiting system:

  • A single Job Position receives applications from hundreds of Candidates.
  • A single Candidate can apply to multiple Job Positions simultaneously.

To connect these two entities without duplicating candidate profiles or position details, an architect creates a custom junction object: Job_Application__c. Each application record links exactly one Candidate to exactly one Position, while storing unique contextual metadata such as application date, interview feedback, and salary requirements.

+-------------------+              +-------------------------+              +-------------------+
|    Position__c    | 1          * |   Job_Application__c    | *          1 |   Candidate__c    |
|   (Master A)      |--------------|    (Junction Object)    |--------------|   (Master B)      |
+-------------------+  Master-     +-------------------------+  Master-     +-------------------+
                       Detail 1                                 Detail 2

Primary Master vs. Secondary Master Architecture

A junction object contains two master-detail relationships. Because custom objects can support a maximum of two master-details, the junction object is fully saturated. Salesforce establishes a hierarchy between these two parents based on creation chronology:

  • Primary Master: The parent object linked by the first master-detail relationship created on the junction object.
  • Secondary Master: The parent object linked by the second master-detail relationship created on the junction object.

Architectural Distinctions Between Masters

Although both master objects function as parents, the Primary Master holds special structural authority over the junction object:

  1. Record Ownership and Look-and-Feel:
    • The junction object inherits its record ownership directly from the Primary Master.
    • Visual styling, including the custom object's default icon, theme color, and tab identity, is inherited from the Primary Master.
  2. Related List Default Placement:
    • When detail records appear in related lists, the Primary Master's page layout displays the junction records with the secondary master's name as a prominent column by default, and vice versa.
  3. Schema Change versus Record Deletion:
    • Deleting an individual master record cascade-deletes its junction records; it never changes which relationship is primary.
    • Deleting the primary master-detail relationship field, or converting it to a lookup, makes the secondary relationship primary. This is a metadata change, subject to dependency restrictions.
FeaturePrimary MasterSecondary Master
Relationship Order1st Master-Detail relationship created2nd Master-Detail relationship created
Ownership InheritanceJunction record inherits owner from PrimaryDoes not govern record ownership
Visual IdentityDictates junction tab color and iconDoes not affect visual theme
Relationship field deleted/convertedSecondary relationship becomes primaryRemaining primary relationship retains its role
Cascade DeletionDeleting primary master deletes junctionDeleting secondary master deletes junction
Roll-Up SummaryFully supports roll-up summariesFully supports roll-up summaries

Cascade Deletion Mechanics in Many-to-Many Relationships

Because a junction record is a detail record in two distinct master-detail relationships, its lifecycle is strictly tied to both parents:

  • Deleting Either Master: If either master record is deleted (whether Position or Candidate), Salesforce automatically executes a cascade delete on all related junction records (Job_Application__c). The junction records are placed in the Recycle Bin.
  • Undeleting a Master: If the deleted master record is restored from the Recycle Bin, the junction records are restored automatically—provided the other master record was never deleted in the interim.
  • Deleting Both Masters: If both master records are deleted independently, the junction record is deleted. In this scenario, the junction record is permanently deleted and cannot be restored.

Note

Deleting one master record deletes the junction record, but has zero impact on the opposite master record. For example, deleting a Position deletes its related Job Applications, but the Candidate records remain completely untouched in the database.


Security, Permissions, and Access Requirements

Security enforcement on junction objects combines the permission structures of both master objects. App builders must remember this compound security model for exam scenarios:

+-----------------------------------------------------------------------------------+
|                         JUNCTION OBJECT ACCESS PERMISSIONS                        |
+-----------------------------------------------------------------------------------+
|  TO VIEW A JUNCTION RECORD:                                                       |
|  User must possess at least READ access on BOTH master objects.                   |
|  (Read on Master A  AND  Read on Master B)                                        |
+-----------------------------------------------------------------------------------+
|  TO CREATE, EDIT, OR DELETE A JUNCTION RECORD:                                    |
|  With Read/Write sharing settings on both relationships, user needs Read/Write on both master records and corresponding junction-object permissions.             |
|  (Both relationship minimums and junction object permissions must be met)                            |
+-----------------------------------------------------------------------------------+

If both relationships require Read/Write access and a user has Read/Write access to Position but only Read access to Candidate, they will be able to see existing Job_Application__c records, but they will be blocked from creating new applications or editing existing ones.


Roll-Up Summaries on Both Master Objects

A major architectural benefit of using two Master-Detail relationships (rather than two Lookups) is that both master objects can support Roll-Up Summary fields calculated from the junction object:

  • On the Position__c object, an app builder can build:
    • A COUNT roll-up calculating total applicants.
    • A MIN roll-up displaying the lowest requested salary among applicants.
    • A MAX roll-up displaying the highest years of experience.
  • On the Candidate__c object, the app builder can build:
    • A COUNT roll-up calculating total active applications submitted.
    • A filtered COUNT roll-up calculating applications in "Interview Scheduled" status.

Best Practices for Designing Junction Objects

  1. Auto-Number Naming: Configure the Record Name field as an Auto-Number (e.g., APP-{00000}) rather than Text, since users should not have to invent artificial names for relational bridge records.
  2. Store Associative Metadata: Use custom fields on the junction object to store information unique to the relationship itself (e.g., Interview_Score__c, Application_Date__c, Status__c), rather than placing these fields on either parent.
  3. Do Not Exceed Two Master-Details: Salesforce strictly limits every custom object to two master-detail relationships. If a business requirement demands linking three or more entities simultaneously (e.g., Project, Employee, and Client), the third relationship must be implemented as a Lookup relationship.

Official Source Check

Checked October 19, 2026: Relationship and junction deletion considerations.

Loading diagram...
Many-to-Many Architecture & Compound Security Boundaries
Test Your Knowledge

Project_Assignment__c links Project__c and Consultant__c through two master-detail fields, both with Sharing Setting Read/Write. A coordinator has Create permission on the junction object and Read access to both parent records, but only Read/Write access to the Project. Why is junction creation blocked?

A

The coordinator does not have the 'Modify All Data' system permission.

B

The Project_Assignment__c object has its Organization-Wide Default set to Private.

C

The coordinator has Read-Only access to one of the parent master objects.

D

The Project_Assignment__c custom tab has not been assigned to the coordinator's profile.

Test Your Knowledge

Course_Enrollment__c has two master-detail relationship fields: Student__c was created first and Course__c second. After removing blocking dependencies, an administrator converts the Student relationship field to a lookup. Which relationship is now primary?

A

Neither relationship, because a lookup cannot coexist with master-detail.

B

The Student lookup remains the primary master-detail relationship.

C

All Course records are cascade-deleted.

D

The remaining Course master-detail relationship becomes primary.

Test Your Knowledge

In a recruiting app, Job_Application__c serves as a junction object between Position__c and Candidate__c using two Master-Detail relationships. A hiring manager accidentally deletes an open Position__c record that had 15 related Job_Application__c records submitted by 12 distinct Candidate__c records. What is the immediate impact on the database?

A

The 15 Job_Application__c records are cascade deleted to the Recycle Bin, while the 12 Candidate__c records remain completely unaffected.

B

Both the 15 Job_Application__c records and the 12 Candidate__c records are cascade deleted into the Recycle Bin.

C

The deletion is blocked because records linked via a junction object cannot be deleted while child records exist.

D

The 15 Job_Application__c records have their Position lookup field cleared to null and remain linked to Candidate__c.

Sections you finish are checked off in the contents.