Sub topic 12
Roll-up Summaries and Parent Record Saves
This chapter assumes you can write a basic Apex trigger, know master-detail and lookup relationships, and understand that a record save runs as one transaction. It covers what happens after the after-save flows finish and a child record's change affects its parent.
What happens after the child saves
When a child record is saved, Salesforce may need to update its parent. There are two common causes:
- The parent has a roll-up summary field (a field that calculates a COUNT, SUM, MIN, or MAX over child records). Roll-up summary fields exist only on the master side of a master-detail relationship.
- Your own automation updates the parent. For example, an after trigger on the child may write to the parent.
In the order of execution, this step comes after after-save flows. If a roll-up summary field is present, Salesforce recalculates it and saves the parent record. That parent save is a separate save inside the same transaction, so it runs its own order of execution.
The parent runs its own automation
The parent save fires the parent's before and after triggers, validation rules, and flows, as any other save does. The difference is that the parent's roll-up value is already recalculated. Two consequences follow:
- A validation rule on the parent can fail because of a child change. The error rolls back the child save too, because both are in the same transaction.
- Triggers on the parent run even though no user edited the parent.
Example: a parent trigger fired by a roll-up
Suppose Account has a roll-up summary field named Total_Open_Amount__c that sums child records. This trigger reacts when the value changes:
trigger AccountRollupWatcher on Account (before update) {
for (Account a : Trigger.new) {
Account oldAcc = Trigger.oldMap.get(a.Id);
if (a.Total_Open_Amount__c != oldAcc.Total_Open_Amount__c) {
a.Description = 'Roll-up changed from '
+ oldAcc.Total_Open_Amount__c + ' to '
+ a.Total_Open_Amount__c;
}
}
}When a child is inserted, this trigger runs on the parent. Comparing Trigger.new with Trigger.oldMap is how you detect that the roll-up caused the update, not a user edit.
Common errors
- Assuming the parent trigger fires only on user edits. It also fires when a roll-up changes.
- Updating the parent from a child trigger without a guard. If the parent's trigger then updates children, you can build a loop that ends in a governor limit error. See the later chapter on recursion guards.
- Expecting a roll-up to be current in a before trigger on the child. The recalculation happens later, so the parent value still reflects the old state.
- Looking for roll-up fields on lookup relationships. Native roll-ups need master-detail. For a lookup, you need a trigger, a flow, or a tool such as Declarative Lookup Rollup Summaries.
Exercise
A user inserts a child record in a master-detail relationship. The parent has a roll-up summary field and a validation rule that fails when the roll-up total exceeds 1000. The new child pushes the total to 1200. What happens to the child record?
Show answer
The roll-up recalculation triggers a parent save, and the parent's validation rule fails. The error rolls back the whole transaction, so the child record is not saved either.