Sub topic 7
Choosing between lookup and master-detail
This chapter assumes you can create custom objects and fields in Setup, and that you know the basic lookup and master-detail behavior from the earlier chapters. Here you compare the two relationship types and pick one for a given requirement.
The behavioral differences
Both relationship types link a child record to a parent record. They differ in how tightly the child depends on the parent.
| Behavior | Lookup | Master-detail |
|---|---|---|
| Parent required on the child | No, unless you mark the field required | Yes, always |
| Deleting the parent | Child survives. The field is cleared by default | Child records are deleted with the parent |
| Child ownership | Child has its own owner | No owner field. Inherited from the parent |
| Child sharing | Set independently | Controlled by the parent |
| Roll-up summary fields | Not available natively | Available on the master object |
| Reparenting the child | Allowed | Blocked unless the field allows reparenting |
| Relationships per object | Up to 40 relationship fields in total, and up to 2 can be master-detail | Same limits |
The reparenting row refers to the reparentableMasterDetail setting you saw in the metadata example. When it is false, a child cannot be moved to a different master after it is created.
A decision flow
Start with the questions that rule out master-detail. Then ask what you gain from it.
Worked scenarios
Contacts and accounts. A contact can exist without an account, and contacts may be shared differently from their account. This needs a lookup.
Invoice line items. A line item has no meaning without its invoice. You want the invoice total calculated from the lines, and you want the lines removed when the invoice is deleted. This needs master-detail with a roll-up summary field.
Support tickets and products. A ticket may or may not refer to a product, and deleting a product must never delete tickets. This needs a lookup.
Working around the lookup limits
Choosing a lookup means giving up native roll-up summary fields. You can still count children with an aggregate query, as in this Apex example. The Id is a placeholder.
Set<Id> accountIds = new Set<Id>{ '001000000000001AAA' };
List<AggregateResult> totals = [
SELECT AccountId, COUNT(Id) total
FROM Contact
WHERE AccountId IN :accountIds
GROUP BY AccountId
];This runs on demand, so the value is not stored on the parent. If you need a stored value, you must write the result to a field yourself, for example from a trigger or a Flow. Alternatively, you can use a managed or declarative roll-up tool.
Common mistakes
- Choosing master-detail for convenience. Teams often pick it only to get a roll-up. Later they find they cannot give the child its own owner or sharing rules.
- Forgetting that cascade delete is permanent in practice. Deleting a master moves its details to the Recycle Bin with it. Treat this as a data-loss risk when users have delete access to the parent.
- Assuming a required lookup equals master-detail. A required lookup blocks empty values, but it still has its own owner, independent sharing, no cascade delete by default, and no roll-ups.
- Ignoring existing data. You can add a master-detail field to an object that already has records only by creating a lookup first, filling it on every record, and then converting it. The next chapter covers this.
Exercise
A Warranty_c object must be deleted whenever its Product_c is deleted, and the product page must show the number of active warranties. Warranties never need their own owner. Which relationship type do you choose, and which feature gives you the count?
Show answer
Master-detail from Warranty_c to Productc. Cascade delete removes warranties with the product, and a roll-up summary field of type COUNT on Product_c, with a filter on active warranties, shows the count.
Exercise
Why would a lookup be the wrong choice if the requirement says "child records must inherit sharing from the parent"?
Show answer
Lookup child records keep their own owner and sharing settings. Only master-detail makes the child's access follow the parent.