Sub topic 4
Master-detail relationships: tightly coupled records
This chapter assumes you know Salesforce objects, fields, and records, can find your way around Setup and the Object Manager, and can read a basic SOQL SELECT. It also assumes you have seen lookup relationships in the previous chapter.
What a master-detail relationship is
A master-detail relationship is a relationship field on a child object. In this relationship type, the child is called the detail and the parent is called the master. The detail record cannot exist without a master. Its life is tied to the master's life.
Three behaviors define it:
- The master field is required. You cannot save a detail record with an empty master field.
- The detail inherits its ownership and sharing from the master. A detail record has no owner field of its own.
- Deleting a master record also deletes its detail records. Chapter 5 covers this cascade in depth.
A lookup is optional and loosely coupled. A master-detail is mandatory and tightly coupled.
Defining the field in metadata
You usually create the field in Setup. In Object Manager, open the detail object, choose Fields & Relationships, click New, and select Master-Detail Relationship. The same field in Salesforce DX source format looks like this:
<?xml version="1.0" encoding="UTF-8"?>
<!-- force-app/main/default/objects/Enrollment__c/fields/Course__c.field-meta.xml -->
<CustomField xmlns="http://soap.sforce.com/2006/04/metadata">
<fullName>Course__c</fullName>
<label>Course</label>
<referenceTo>Course__c</referenceTo>
<relationshipLabel>Enrollments</relationshipLabel>
<relationshipName>Enrollments</relationshipName>
<relationshipOrder>0</relationshipOrder>
<reparentableMasterDetail>false</reparentableMasterDetail>
<type>MasterDetail</type>
<writeRequiresMasterRead>false</writeRequiresMasterRead>
</CustomField>These elements matter most:
| Element | Purpose |
|---|---|
| `referenceTo` | The master object. |
| `relationshipName` | The name used to reach the child records in a parent-to-child SOQL query. |
| `relationshipOrder` | `0` makes this the primary master. A second master-detail field on the same object uses `1`. |
| `reparentableMasterDetail` | If `true`, users can change the master of an existing detail record. The default is `false`. |
| `writeRequiresMasterRead` | Controls the access a user needs on the master to create or edit detail records. With `false`, the user needs Read on the master. With `true`, the user needs Read and Edit. |
Reparenting
By default, a detail record stays with the master it was created under. Setting reparentableMasterDetail to true (the Allow reparenting checkbox in Setup) lets users pick a different master on an existing detail record. Leave it off when the detail's identity depends on its master, for example when sharing or approval history should not move between masters.
Constraints to plan for
Master-detail has limits that lookups do not. Check them before you choose it:
- The master field is always required, so you cannot make it optional.
- A custom object can have at most two master-detail relationships. This limit is why a junction object has exactly two masters.
- A standard object cannot be the detail side of a master-detail relationship. A custom object can be the detail of a standard object such as Account.
- Existing records limit conversion. If you add a master-detail field to an object that already has data, every record needs a value in the field first. Chapter 8 covers this in detail.
Common errors
- Required field missing. Inserting a detail record without its master Id fails with
REQUIRED_FIELD_MISSING. Insert the master first, then use its Id. - Access error on create. A user who can create the detail object but cannot read the master gets an insufficient-access error. Check
writeRequiresMasterReadand the user's access to the master. - Unexpected deletes. Deleting a master removes all its details. Check the child relationships before a bulk delete.
- No Owner field in reports or list views. Detail records have no owner. Use the master's owner instead.
Exercise
A developer adds a master-detail field from Enrollment__c to Course__c and sets reparentableMasterDetail to false. A user opens an existing enrollment and wants to move it to another course. What happens, and which setting would change it?
Show answer
The user cannot change the Course value on the existing enrollment, because reparenting is off. Setting reparentableMasterDetail to true (Allow reparenting in Setup) lets users select a different master on existing detail records.
Exercise
Why can a custom object never have three master-detail fields, and what is the practical consequence for a junction object?
Show answer
Salesforce allows at most two master-detail relationships per custom object. A junction object therefore has exactly two masters, one for each side of the many-to-many relationship.