# What aggregate calculations can Roll-Up Summary fields perform across Master-Detail child records?

**Topic:** Admin · **Difficulty:** Medium · **Tags:** Admin, Roll-Up Summaries, Aggregations

Read it on the site: https://www.forcechallenges.com/interview-questions/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:**
1. On the master object: Fields & Relationships > New > Roll-Up Summary.
2. Choose the detail object (from the master-detail relationships pointing at this object).
3. 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).
4. Optionally set filter criteria ("only records meeting criteria").
5. 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:**
```text
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.
- Up to 25 roll-up summary fields per object.
- They require master-detail; converting a lookup requires every child to have a parent first.
- Roll-ups cannot be used to sum certain formula fields that reference other objects or other roll-ups; check restrictions when a field is not listed as selectable.
- After changing filter criteria or the summarized field, run the "Calculate Related Roll-up Summary Fields" option on the parent field to recompute existing data.
- Large child counts per parent cause row locking, so sort and batch data loads by parent.

**Best practice:** Use native roll-ups when relationship type allows, and avoid stacking many on one heavily loaded parent.

---
Source: ForceChallenges · last updated 2026-11-02
