Sub topic 2
Cascade Delete, Ownership, and Sharing in Master-Detail
Assume you know Salesforce objects, fields, records, and basic SOQL. This chapter focuses on how tightly coupled relationships affect data access and design.
Master-detail behavior
A master-detail relationship makes the detail record dependent on its master.
- Deleting the master cascades to its detail records. Cascade delete means Salesforce deletes the related detail records automatically.
- A detail record must have a master. The parent reference is required.
- The detail does not have independent ownership. It inherits ownership and sharing access from the master.
- Sharing rules and manual sharing applied to the master control access to its detail records.
This behavior is useful when a detail record has no meaning outside its parent. It is risky when users must keep the detail record after the parent is deleted.
Roll-up summary fields
A roll-up summary field belongs on the master object. It calculates values from related detail records. Supported calculations include:
| Type | Example |
|---|---|
| COUNT | Number of related detail records |
| SUM | Total of a currency or number field |
| MIN or MAX | Smallest or largest date or number |
Roll-ups support filters, so a master can count only detail records that meet a condition. Native roll-ups require a master-detail relationship. For a lookup, use automation or an aggregate query instead.
Choosing the relationship type
Choose a lookup when the link is optional and both records should have independent lifecycles. Choose master-detail when the child requires the parent, should inherit access, and should be deleted with it.
Converting relationships
You can convert a lookup to master-detail when every existing child has a valid parent and the relationship supports conversion. Existing records without a parent block the change because the new parent field is required. Unsupported object combinations, conflicting configuration, or dependent metadata can also prevent conversion.
Converting master-detail to lookup removes the tightly coupled behavior. Review roll-up fields, sharing assumptions, automation, and data retention before changing the type.
Junction objects
A junction object models a many-to-many relationship. For example, Enrollment__c can connect Student__c and Course__c with two master-detail fields. Each enrollment requires both parents. The primary master controls ownership and sharing. Deleting either master can delete the junction records connected to it.
Exercise
Which relationship should you choose when a child must inherit sharing and be deleted with its parent?
Show answer
Choose a master-detail relationship.