Sub topic 11
Primary and Secondary Master: What Each One Controls
This chapter assumes you can create custom objects and fields in Object Manager. It also assumes you know what a master-detail field does and have seen a junction object with two master-detail fields. It covers what differs between the two parents of a junction object, which behaviors follow the primary master, and which apply to both masters.
Two masters, two roles
A junction object has exactly two master-detail fields. Salesforce gives them different roles:
- Primary master: the first master-detail field created on the junction object.
- Secondary master: the second master-detail field created on the junction object.
In Setup, the order of creation decides the roles. You can't pick a role in the field wizard. In metadata, the role is stored in the relationshipOrder element of the field. A value of 0 means primary and 1 means secondary.
The junction object has one detail side and two master sides. A normal detail record has one parent, which supplies its ownership, sharing, and look and feel. A junction record has two parents, so Salesforce has to choose one parent for the things that can only come from one place. That parent is the primary master.
What the primary master controls
The primary master decides these things for junction records.
Inherited owner
A junction record has no editable Owner field, as with any detail record in a master-detail relationship. Internally it takes its owner from its primary master record. The owner is not visible on the record. It matters only in rare cases, for example if you later delete both master-detail fields from the object.
Look and feel
The detail and edit pages of a junction record use the color and icon of the primary master object. If Course is primary, Enrollment records look like Course records in the UI. This is cosmetic, but it can confuse users when the primary master is the object they think of as secondary.
Division
If your org uses divisions, a junction record inherits its division from the primary master.
Promotion when the primary field is deleted
If you delete the primary master-detail field, the secondary becomes the new primary. If you then add a new master-detail field, it becomes the secondary. Deleting and recreating a field is the supported way to change which parent is primary. Plan this carefully, because deleting a field deletes its data.
What the secondary master controls
Very little differs for the secondary master. It still has all the master behaviors that matter for data integrity:
- It is required on every junction record.
- It can have roll-up summary fields over the junction object.
- Deleting a secondary master record deletes its junction records.
- It contributes to the junction record's visibility, as described below.
What both masters control
Several behaviors treat the two parents the same. Teams often wrongly assume the primary master owns them.
Required parents
Both master-detail fields are required. You can't insert a junction record with only one parent, so both parent records must exist first. The Apex example from the spine shows this order:
Course__c course = new Course__c(Name = 'Apex Basics');
Student__c student = new Student__c(Name = 'Ada Lovelace');
insert course;
insert student;
insert new Enrollment__c(Course__c = course.Id, Student__c = student.Id);Leaving either Course__c or Student__c null makes the insert fail with a required-field error.
Cascade delete from either side
Deleting either master record deletes its junction records. Deleting a course removes its enrollments. Deleting a student removes that student's enrollments. The other master record is not affected. Deleting an Enrollment__c record removes the link and leaves both parents.
Visibility and sharing
A junction record is not visible just because someone can see one parent. Access to a junction record is based on the user's access to both masters, and the strictest result wins. A user who can see the course but not the student can't see that enrollment.
Each master-detail field also has a Sharing Setting that controls how much access to the master is needed before a user can create, edit, or delete junction records. In metadata it is the writeRequiresMasterRead element:
| `writeRequiresMasterRead` | Setup label | Access to the master needed to create, edit, or delete the junction record |
|---|---|---|
| `false` (default) | Read/Write | Read/Write on the master |
| `true` | Read Only | Read on the master |
Each field has its own setting, so the two masters can differ. A user with read-only access to a student record, whose field is set to Read Only, can create enrollments for that student. The same user can't enroll anyone in a course where the field is set to Read/Write unless they can edit that course.
The secondary field in metadata
The spine example defined the primary master field on Enrollment__c. Here is the matching secondary field on Student__c. Only relationshipOrder and the names differ.
<?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>Two details to check when you write this by hand:
- A junction object needs
relationshipOrdervalues of0and1, one each. Two fields with the same value will not deploy. - Setting
reparentableMasterDetailtotruelets users change the parent on an existing junction record. With the defaultfalse, a junction record keeps its parents for its whole life. To move a student to a different course, delete the enrollment and create a new one.
Querying through both masters
The role of a master does not change how you query the junction object. You can reach both parents from the junction side with dot notation on the __r relationship names:
SELECT Id, Course__r.Name, Student__r.Name
FROM Enrollment__cFrom a parent, a subquery uses the child relationship name you set on the field. Both fields above use Enrollments, which is allowed because each name only has to be unique on its own parent object:
SELECT Name, (SELECT Student__r.Name FROM Enrollments__r)
FROM Course__c
WHERE Name = 'Apex Basics'The primary and secondary roles don't change these queries. They affect the record's ownership, look and feel, and field-deletion behavior, not the query syntax.
Roll-up summaries on both parents
Both masters can have roll-up summary fields over the junction object. A Course can count its enrollments, and a Student can count theirs. This is one reason to use master-detail on both sides of a junction object instead of a lookup. A lookup field can't support a native roll-up summary field.
Choosing which parent is primary
The choice is cosmetic and operational, not structural. Use these questions to decide:
- Which object do users start from when they work with the linked records? That object is a good primary master, because junction records will look like it.
- Which parent's division matters, if you use divisions? Make it primary.
- Will you ever delete one of the fields? If so, remember that the secondary is promoted.
If none of these apply, the choice does not affect the data model. What you must get right is the pair of sharing settings and the access model for each master.
Common mistakes
- Expecting a role switch in the UI. There is no toggle. Changing roles means removing a field, which deletes its data. Back up junction data before you try it.
- Assuming the primary master alone controls visibility. Users need access to both masters to see a junction record.
- Creating the junction record before its parents. Both parent Ids are required at insert.
- Deploying two fields with the same
relationshipOrder. The deployment fails. Use0and1. - Assuming sharing settings are the same on both fields. Each field has its own
writeRequiresMasterReadvalue.
Exercise
A junction object Enrollment_c has master-detail fields to Coursec (relationshipOrder 0) and Studentc (relationshipOrder 1). A user can read and edit every Coursec record. They can read Studentc records but not edit them. The Student_c field has writeRequiresMasterRead set to false. Can the user create an enrollment? What changes if you set that field to true?
Show answer
No. With writeRequiresMasterRead set to false, the user needs Read/Write access to the master record, and they only have read access to the student. The user needs access to both masters, and the Student__c field blocks the insert. If you set the field to true, only Read access to the student is required. Read access to the student plus edit access to the course then lets the user create the enrollment. Which master is primary makes no difference here.