What aggregate calculations can Roll-Up Summary fields perform across Master-Detail child records?
Answer
Basics: A roll-up lets a parent display totals from its children without code, such as number of Order Lines, total Line Amount or the latest Child Date on the parent.
How it works:
- On the master object: Fields & Relationships > New > Roll-Up Summary.
- Choose the detail object (from the master-detail relationships pointing at this object).
- Choose the type: COUNT; SUM, MIN or MAX on a number, currency or percent detail field (MIN and MAX also work on date and date/time fields).
- Optionally set filter criteria ("only records meeting criteria").
- Salesforce recalculates when children are created, edited, deleted, or reparented, and when relevant fields change.
Advanced / deep dive: Standard objects with built-in roll-ups include Opportunity to Opportunity Products. Roll-ups are calculated synchronously in the child transaction, and updating the parent value causes the parent record to go through its save process, so parent triggers, validations and flows can run in that transaction. This can cause UNABLE_TO_LOCK_ROW errors when many children of one parent are loaded in parallel. For averages, use a formula on the parent (Total__c / Count__c with a null guard). For a lookup relationship, use a record-triggered flow, an Apex trigger with aggregate queries, or the open-source Declarative Lookup Rollup Summaries package. CRM Analytics or reports handle ad hoc aggregation without storing results.
Code / config example:
Parent field: Total_Line_Amount__c (Roll-Up Summary, SUM of OrderLine__c.Amount__c)
Parent formula for average:
IF( Line_Count__c > 0, Total_Line_Amount__c / Line_Count__c, 0 )
Hidden gotchas & must-remember points:
- Only COUNT, SUM, MIN, MAX exist; no average, median or distinct count.