Sub topic 14
Roll-up Summary Fields on Master-Detail Parents
This chapter assumes you know what a master-detail relationship is, how to create a custom field in Object Manager, and the basic numeric and date field types. A roll-up summary field is a field on a parent record that calculates a value from its child records. It saves you from writing code or flows to keep totals current.
What a roll-up summary field does
A roll-up summary field lives on the master (parent) object of a master-detail relationship. It summarizes the detail (child) records. You choose one of four roll-up types:
| Roll-up type | Works on | Result |
|---|---|---|
| COUNT | Child records | Number of related children |
| SUM | Number, Currency, Percent fields | Total of the child field |
| MIN | Number, Currency, Percent, Date, Date/Time fields | Smallest or earliest value |
| MAX | Number, Currency, Percent, Date, Date/Time fields | Largest or latest value |
The field is read-only and recalculates automatically when child records are created, edited, deleted or undeleted. You can also add a filter so that only some children are counted. For example, you could sum only child records where a status equals "Approved".
Why master-detail is required
Roll-up summary fields exist only on the master side of a master-detail relationship. In a master-detail relationship, the child record cannot exist without its parent, and the parent controls its sharing and deletion. That tight link lets the platform recalculate the total reliably.
A lookup relationship is looser. The child may have no parent at all, so the platform does not offer roll-up summary fields there. The roll-up summary field type does not appear in the list when the object has no master-detail children.
Choosing the rollup field to summarize
Pick the type that matches the question you want answered:
- How many contacts does an account have? Use COUNT.
- What is the total value of all line items on an order? Use SUM on the Currency field.
- When is the most recent activity? Use MAX on a Date/Time field.
Common limits and mistakes
- Relationship type: You cannot create a roll-up on a lookup. You can change a lookup to master-detail only if every child already has a parent value.
- Field limit: An object can have a limited number of roll-up summary fields (25 by default). Check your edition's limits before planning many of them.
- Formula dependencies: A roll-up cannot summarize a formula field that references another object or uses certain functions. Test the formula you plan to summarize.
- Locked relationship: Once a roll-up summary field depends on the relationship, you cannot easily convert the master-detail back to a lookup. You must delete the roll-up first.
- Currency: In multi-currency orgs, the roll-up converts child amounts to the parent's currency.
When a lookup is unavoidable
If the relationship must stay a lookup, compute the total with an aggregate query:
List<AggregateResult> totals = [
SELECT AccountId, SUM(Amount) total
FROM Opportunity
WHERE AccountId != null
GROUP BY AccountId
];
for (AggregateResult ar : totals) {
System.debug(ar.get('AccountId') + ': ' + ar.get('total'));
}This gives the same totals, but you run it yourself and it is not stored on the parent record unless you save it.
Exercise
Your Project__c object has a lookup to Account, and you want each Account to show the total of Project budgets. Why can't you add a roll-up summary field, and what are two ways to get the total?
Show answer
Roll-up summary fields require a master-detail relationship, and a lookup does not qualify. You can convert the lookup to master-detail (only if every project has an account, and you accept the shared sharing and deletion behavior), or you can compute the total with an aggregate SOQL query in Apex or a flow.