Sub topic 6
Summarizing child records with roll-up summary fields
This chapter assumes you know how to create custom objects and fields in Setup, and that you have read the earlier chapters on lookup and master-detail relationships. You should also be comfortable with basic SOQL.
What a roll-up summary field does
A roll-up summary field is a field on a master object that calculates a value from its related detail (child) records. It updates automatically when child records are created, edited, or deleted. You don't write any code to keep it current.
Roll-up summary fields exist only on the master side of a master-detail relationship. A lookup relationship cannot have a native roll-up summary field. This is one of the main reasons teams choose master-detail.
Supported roll-up types
| Roll-up type | What it returns | Field types it can summarize |
|---|---|---|
| COUNT | Number of related child records | None needed |
| SUM | Total of a child field | Number, currency, percent |
| MIN | Smallest value of a child field | Number, currency, percent, date, date/time |
| MAX | Largest value of a child field | Number, currency, percent, date, date/time |
Creating a roll-up summary field
Suppose Enrollment__c is a detail object of Course__c, and Enrollment__c has a currency field Fee__c. To total the fees on each course:
- Open Object Manager and select the master object,
Course__c. - Go to Fields & Relationships and click New.
- Choose Roll-Up Summary as the data type.
- Name the field, for example
Total Fees. - Select
Enrollment__cas the summarized object. Only detail objects of this master appear in the list. - Choose SUM and select
Fee__cas the field to aggregate. - Optionally choose to count only records that meet filter criteria, such as
Status__cequalsConfirmed. - Save and add the field to the page layout.
Filters and what they change
A roll-up can include all child records or only those matching filter criteria. Filters let you build several roll-ups on one master. For example, you could keep one count of all enrollments and another of only confirmed ones.
Filter conditions are evaluated against the child record. If a child record is edited so it no longer matches the filter, the roll-up recalculates and excludes it.
Limits and common errors
- Master-detail required. If the relationship is a lookup, Roll-Up Summary doesn't appear as a data type. Convert the relationship first, as covered in a later chapter.
- Per-object limit. An object can have a limited number of roll-up summary fields. The default is 25, and some editions allow fewer.
- Calculation timing. Roll-up values are recalculated when child records change. Changing a roll-up's definition can trigger a recalculation of existing data, and this may take time on large data volumes.
- Formulas and cross-object chains. Roll-up summary fields have restrictions on what they can reference. A roll-up cannot summarize a formula field that references another roll-up on a different object in a way that creates a loop. Salesforce blocks definitions that would create circular dependencies.
- Field type mismatch. SUM works only on numeric types. If the field you need isn't listed, check its data type.
- Sharing is ignored. A roll-up counts all child records, even ones the viewing user cannot see. The value on the master can therefore include data the user has no access to.
When you only have a lookup
If the relationship is a lookup and you can't convert it, you can calculate totals yourself. One option is an aggregate SOQL query:
Set<Id> accountIds = new Set<Id>{ '001000000000001AAA' };
List<AggregateResult> totals = [
SELECT AccountId, COUNT(Id) total
FROM Contact
WHERE AccountId IN :accountIds
GROUP BY AccountId
];The Id above is a placeholder. Replace it with real account Ids. The query returns one row per account, with the count in the total alias. You can read it with (Integer) totals[0].get('total').
This gives you a number at query time. It does not store the value on the parent record. To store it, you would need a trigger or a Flow that writes the result into a regular number field.
Exercise
A Project_c object has a lookup field to Account. A manager wants each Account to display the total of Projectc.Budget_c. Why can't you add a SUM roll-up summary field to Account, and what are two ways to get the total?
Show answer
Roll-up summary fields work only on the master side of a master-detail relationship, and a lookup doesn't allow them. You can convert the lookup to master-detail, if no blocking conditions exist, and then create the SUM roll-up. Or you can keep the lookup and calculate the total with an aggregate SOQL query, or with a trigger or Flow that stores the result in a number field on Account.
Exercise
Which roll-up types can summarize a date field?
Show answer
MIN and MAX. COUNT needs no field, and SUM works only on number, currency, and percent fields.