Sub topic 10
Why roll-ups need a master-detail relationship
This chapter assumes you know the Salesforce data model (objects, fields, records), can navigate Setup and the Object Manager, and have seen lookup and master-detail relationships at a high level. It explains why roll-up summary fields only exist on the master side of a master-detail relationship, and what that requirement means in practice.
What a roll-up summary field needs
A roll-up summary field calculates a value from a set of child records and stores it on the parent record. For example, an Invoice__c record can hold the total of all its Line_Item__c amounts.
For that to work, Salesforce must be able to answer two questions reliably:
- Which child records belong to this parent?
- When a child changes, which parent must be recalculated, and can that happen safely?
A master-detail relationship answers both. A lookup relationship does not. Roll-up summary fields are available only on the master object of a master-detail relationship. The Roll-Up Summary field type does not appear in the field type list for a lookup parent.
Master-detail versus lookup
| Property | Master-detail | Lookup |
|---|---|---|
| Parent value on child | Required. A detail record cannot exist without a master. | Optional by default. |
| Deleting the parent | Deletes all detail records (cascade delete). | Children stay, and the lookup field is cleared by default. |
| Ownership and sharing | The detail record inherits owner and sharing from the master. | The child has its own owner and sharing. |
| Roll-up summary fields | Supported on the master. | Not supported. |
| Reparenting | Blocked by default. It must be allowed on the field. | Always allowed. |
The first three rows are what make roll-ups safe. A detail record always has exactly one master, so every child maps to a parent. A detail record is deleted with its master, so the parent never holds a total that includes orphaned children. Because the detail inherits sharing from the master, the roll-up never mixes records that different users are allowed to see.
How the relationship shapes the calculation
The roll-up is computed by the platform, not by your code. When a detail record is inserted, updated, or deleted, Salesforce finds the master through the required master-detail field and recalculates the summary. Because that field is never blank, the platform does not have to handle detail records with no parent.
The following diagram shows the flow when a line item is saved.
The locking step in the diagram matters. Because the master record is updated whenever a child changes, many simultaneous child edits for the same master can contend for that lock. Later chapters cover recalculation and locking in detail. For now, note that the relationship type makes this behavior possible.
Creating the relationship
You create the master-detail field on the detail object. A typical setup for this tutorial's invoice example:
- Open Setup, then Object Manager, then
Line_Item__c. - Choose Fields & Relationships, then New.
- Select Master-Detail Relationship as the data type.
- Choose
Invoice__cas the related object. - Set the sharing and reparenting options, then add the field to page layouts.
The field you create is the one used in the roll-up wizard. In this example it is the field named Invoice__c on Line_Item__c.
Reparenting
By default, a detail record cannot be moved to a different master. If your process needs it, select "Allow reparenting" on the master-detail field. When a child moves, Salesforce recalculates the roll-up on both the old and the new master.
Sharing on the detail
You choose the sharing access that applies to the master record when you set up the relationship. The detail object does not get its own owner field. Its visibility is controlled through the master, which keeps roll-up results consistent for everyone who can see the master.
Constraints that come with master-detail
Choosing master-detail to get a roll-up has costs. Check these before you commit.
- Existing data. You can create a master-detail field only if the detail object has no records without a value, or you first populate a lookup on every record. A common approach is to create a lookup, fill it on every existing record, then convert it to master-detail.
- Converting back. You can convert master-detail to lookup only if the master object has no roll-up summary fields. You must delete those fields first.
- Cascade delete. Deleting an invoice deletes its line items. That is the intended behavior, but it can surprise teams who expected children to survive.
- Standard objects. A standard object cannot be the detail side of a master-detail relationship to a custom object. Standard objects can be masters. For example, you can roll up custom child records onto some standard objects, but not all standard objects support it.
- Relationship depth. An object can take part in a limited chain of master-detail relationships. Treat deep chains as a design smell.
- Many-to-many. A junction object has two master-detail fields. You can roll up from the junction to each of its two masters.
Converting a lookup to master-detail
Existing orgs often have a lookup where a roll-up is now wanted. The conversion works only if every existing child record has a value in the lookup field.
- Find child records with a blank lookup. A SOQL query does this:
SELECT Id, Name FROM Line_Item__c WHERE Invoice__c = null- Assign a parent to each one, or delete the records that should not exist.
- In Setup, edit the lookup field and change the data type to Master-Detail Relationship.
- Create the roll-up summary field on the master.
After conversion, the field becomes required, the detail takes on the master's sharing, and any existing code that inserts children without a parent starts failing.
When master-detail is the wrong choice
Master-detail is a strong coupling. Avoid it when:
- Children must exist without a parent, such as optional contacts or tasks.
- Children need their own owner or independent sharing rules.
- The parent is a standard object that does not allow master-detail children.
- You need to aggregate across a lookup that already carries data you cannot change.
For those cases, the later chapter on alternatives covers declarative tools and Apex that aggregate children on lookup relationships.
Verifying the setup
After you create the relationship and the roll-up, confirm that it works by reading the value from the master. Roll-up values cannot be set in DML, so you query them.
Invoice__c inv = new Invoice__c(Name = 'INV-001');
insert inv;
insert new Line_Item__c(Invoice__c = inv.Id, Amount__c = 40);
Invoice__c saved = [SELECT Id, Total_Amount__c FROM Invoice__c WHERE Id = :inv.Id];
System.debug(saved.Total_Amount__c);The debug output shows the platform-calculated total. If you read inv.Total_Amount__c from the in-memory record instead, the value is blank, because the in-memory object was never refreshed after the child insert.
Exercise
A team has a lookup field from Payment__c to Account, and 5,000 existing payments. Some payments have no account. They want a SUM roll-up of payment amounts on Account. What must they do before they can convert the lookup to master-detail, and what happens to the roll-up option until then?
Show answer
Every existing payment must have a value in the lookup field. They need to find the payments with a blank account (for example, with a SOQL query filtering on null), assign each one an account or delete it, and then change the field type to Master-Detail Relationship. Until the conversion is done, the Roll-Up Summary field type is not offered on Account for this relationship, because roll-ups are only supported on the master side of a master-detail relationship.
Exercise
Why does a master-detail relationship make roll-ups safe where a lookup would not? Give two properties of master-detail that you rely on.
Show answer
Every detail record has exactly one required master, so each child maps to a parent. Deleting the master deletes its details, so the stored total never includes orphaned children. The detail also inherits sharing from the master, so the roll-up is consistent for everyone who can see the master. Any two of these properties is a correct answer.