Sub topic 5
Cascade delete, ownership, and sharing in master-detail
This chapter assumes you can create fields in Object Manager and know the basics of record ownership and sharing settings. It covers three behaviors that follow from a master-detail field: deletion flows from master to detail, the detail has no owner of its own, and the detail inherits its access from the master.
Cascade delete
When you delete a master record, Salesforce deletes all of its detail records in the same operation. Deleting a detail record never deletes the master.
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);
delete course;
// The enrollment was deleted with the course. The student remains.
System.debug([SELECT COUNT() FROM Enrollment__c WHERE Student__c = :student.Id]); // 0
System.debug([SELECT COUNT() FROM Student__c WHERE Id = :student.Id]); // 1Enrollment__c has two master-detail fields. Deleting either parent therefore deletes the junction record. Deleted details go to the Recycle Bin together with their master. If you run undelete course;, Salesforce restores the details as well.
Required parent
The master-detail field is always required on the detail record. You cannot save a detail without a master, and you cannot clear the field later. By default you also cannot move a detail to a different master. Setting reparentableMasterDetail to true on the field, as in the earlier metadata example, allows reparenting.
Ownership
Detail records have no OwnerId field of their own. The owner of a detail record is always the owner of its master. If the master's owner changes, the owner of every detail changes with it.
This has practical effects:
- Apex code and data loads cannot set
OwnerIdon a detail record. - Ownership-based sharing rules cannot be created for the detail object.
- Reports and list views show the master's owner for each detail.
Inherited sharing
The detail object's organization-wide default is Controlled by Parent. A user can see a detail record only if they can see its master. Sharing the master, by sharing rule, manual share, or role hierarchy, shares its details automatically. You cannot share a detail record independently.
The field's Child Record Access setting controls what a user needs on the master to create, edit, or delete details. It maps to writeRequiresMasterRead in metadata:
| `writeRequiresMasterRead` | Access needed on the master to change details |
|---|---|
| `false` (default) | Read |
| `true` | Read/Write |
The user also needs the right object permission on the detail object. The setting adds a requirement on top of that permission and does not replace it.
Contrast with lookup
A lookup child keeps its own owner and its own sharing. Its parent field can be empty, and deleting the parent does not delete the child. The master-detail behaviors in this chapter are the price of tighter coupling.
| Behavior | Lookup | Master-detail |
|---|---|---|
| Parent required | No | Yes |
| Child has its own owner | Yes | No |
| Child sharing | Independent | Controlled by parent |
| Deleting the parent | Configurable (clear the field or block deletion) | Deletes the children |
Common errors
- Owner field missing. You cannot add
OwnerIdto a page layout or data load for a detail object. Change the master's owner instead. - Surprise data loss. Deleting a master removes every detail. Check the child count before bulk deletes.
- Users who cannot see details. The cause is almost always missing access to the master. Check the master's sharing first, not the detail's.
- Users who cannot edit details. With
writeRequiresMasterReadset totrue, Read access to the master is not enough.
Exercise
A user has Read access to a Course_c record and Create and Edit permission on Enrollmentc. The Coursec field on Enrollment_c has writeRequiresMasterRead set to true. Can the user add an enrollment to that course? Who owns the new enrollment?
Show answer
No. With writeRequiresMasterRead set to true, creating a detail requires Read/Write access to the master, and the user only has Read. If access were sufficient, the enrollment would have no owner of its own. Its owner is the owner of the Course__c master.