Sub topic 11
Primary and secondary master: what each one controls
This chapter assumes you can create master-detail fields in Setup and know what a junction object is. A junction object is a custom object with two master-detail fields, one to each parent. Here you will see how the two fields differ in what they control.
Two master-detail fields, two roles
A junction object has exactly two master-detail relationships. The first one you create is the primary master. The second one is the secondary master. In metadata, the relationshipOrder element records this: 0 is the primary master and 1 is the secondary master.
In the example schema, Enrollment__c joins Course__c and Student__c. If Course__c is the primary master, then Student__c is the secondary master.
<?xml version="1.0" encoding="UTF-8"?>
<!-- force-app/main/default/objects/Enrollment__c/fields/Student__c.field-meta.xml -->
<CustomField xmlns="http://soap.sforce.com/2006/04/metadata">
<fullName>Student__c</fullName>
<label>Student</label>
<referenceTo>Student__c</referenceTo>
<relationshipLabel>Enrollments</relationshipLabel>
<relationshipName>Enrollments</relationshipName>
<relationshipOrder>1</relationshipOrder>
<reparentableMasterDetail>false</reparentableMasterDetail>
<type>MasterDetail</type>
<writeRequiresMasterRead>false</writeRequiresMasterRead>
</CustomField>What the primary master controls
The primary master decides the junction record's inherited behavior:
- Sharing and ownership. The junction record has no owner of its own. Its access follows the primary master. A user who can see the course can see its enrollments, subject to the other parent's rules described below.
- Look and feel. The junction object's page layout and related-list placement follow the primary master.
- Deletion order. If you delete the primary master, the junction records are deleted along with it.
What the secondary master controls
The secondary master has less influence. It does not decide ownership or sharing. It still carries the master-detail guarantees:
- The junction record cannot exist without it, because the field is required.
- Deleting the secondary master deletes its junction records too.
- Roll-up summary fields can summarize junction records onto it, as they can on the primary master.
If you delete the primary master of a junction record, the junction record is deleted. If you delete only the secondary master, the junction records for that parent are deleted. Either way, the other parent record stays.
Deleting a master and restoring it
Deleting a junction record never deletes either parent. Deleting a parent removes its junction records, and they go to the Recycle Bin together with the parent. Undeleting the parent restores them.
One edge case matters. If you delete both masters of a junction record, the junction record is deleted permanently when the second parent is deleted. In that case, undeleting one parent will not bring the junction record back.
What happens when the primary master is removed
You cannot delete a master-detail field that is the primary master while the secondary one exists, without first dealing with the order. If you delete the primary master field, the secondary master field is promoted to primary master. That promotion changes the sharing behavior of every junction record, so check access before you do it.
Choosing which parent is primary
Pick the primary master from the sharing question: whose access should govern the junction records?
| Question | Guidance |
|---|---|
| Whose sharing should the junction records follow? | Make that parent the primary master. |
| Which parent's page should show the related list first? | Usually the primary master. |
| Does it matter for roll-up summaries? | No. Both masters support them. |
For the enrollment example, if an instructor who owns a course should see all enrollments in it, make Course__c primary.
Checking the roles with a query
The roles do not change how you query. Both relationship names work the same way.
SELECT Name, (SELECT Student__r.Name FROM Enrollments__r)
FROM Course__c
WHERE Name = 'Apex Basics'Exercise
A junction object Enrollment__c has master-detail fields to Course__c (primary) and Student__c (secondary). Which parent decides who can see an enrollment record, and what happens to enrollments when a Student__c record is deleted?
Show answer
The primary master, Course__c, determines the junction record's sharing and ownership. When a Student__c record is deleted, the enrollments that belong to that student are deleted too, while the courses stay.