Sub topic 13
Roll-up summaries and parent record saves
This chapter covers the step after the after-save flows have finished. Salesforce recalculates roll-up summary fields on the parent record and saves the parent. The parent's own automation then runs. This step explains several behaviors that look odd, such as a child record failing because of an error on its parent. It assumes you know master-detail relationships and basic Apex triggers.
What happens in this step
A roll-up summary field is a field on the parent object of a master-detail relationship. It calculates a COUNT, SUM, MIN, or MAX over child records. When a child record is inserted, updated, or deleted, the parent's roll-up value may change.
Salesforce handles this late in the transaction:
- The child record has been saved to the database, but not committed.
- The child's after triggers, workflow rules, and after-save flows have run.
- Salesforce recalculates the roll-up summary fields on the parent.
- If a roll-up value changed, the parent record goes through the save procedure. That procedure includes the parent's before triggers, validation, after triggers, and flows.
- If the parent has its own roll-up summary parent (a grandparent), the same step repeats upward.
- Criteria-based sharing is evaluated, then the transaction commits.
The parent is saved as part of your transaction
The parent save is not a separate transaction. It runs inside the same one as the child save. Three consequences follow:
- Parent triggers see the new roll-up value in
Trigger.new.Trigger.oldMapholds the previous value. - An error on the parent rolls back the child too. A parent validation rule, a parent trigger calling
addError(), or an uncaught exception can all do this. - Governor limits are shared. The parent's SOQL queries and DML count against the same totals as the child's.
| Question | Answer |
|---|---|
| Does the parent's trigger fire? | Yes, when the roll-up value changes the parent. |
| Is it a new transaction? | No. It is the same transaction as the child save. |
| Can a parent failure undo the child save? | Yes. The whole transaction rolls back. |
| Which relationships qualify? | Master-detail relationships. A lookup relationship has no built-in roll-up summary field. |
Detecting a roll-up change in a parent trigger
A parent trigger fires for many reasons. To react only to a roll-up change, compare old and new values. This example assumes an Invoice__c parent with a roll-up summary field Total_Amount__c over Line_Item__c children, plus a text field Status__c.
trigger InvoiceRollupWatch on Invoice__c (before update) {
for (Invoice__c inv : Trigger.new) {
Invoice__c oldInv = Trigger.oldMap.get(inv.Id);
if (inv.Total_Amount__c != oldInv.Total_Amount__c) {
Boolean large = inv.Total_Amount__c != null && inv.Total_Amount__c > 10000;
inv.Status__c = large ? 'Needs Approval' : 'Open';
}
}
}This is a before trigger on the parent, so it changes Status__c with no DML. The change is saved with the parent in this same step.
The stale value in the child's after trigger
The child's after trigger runs before the roll-up is recalculated. If it queries the parent, it reads the old roll-up value.
trigger LineItemStaleRead on Line_Item__c (after insert) {
Set<Id> invoiceIds = new Set<Id>();
for (Line_Item__c li : Trigger.new) {
invoiceIds.add(li.Invoice__c);
}
for (Invoice__c inv : [SELECT Id, Total_Amount__c FROM Invoice__c WHERE Id IN :invoiceIds]) {
System.debug('Roll-up seen in child after trigger: ' + inv.Total_Amount__c);
}
}The debug line prints the total as it was before the new line items. The new total exists only once the step in this chapter has run. If you need the new total in the child's trigger, calculate it yourself from the child records. Alternatively, put the logic in the parent's trigger, which sees the final value.
Parent validation and the child save
A parent record can be perfectly valid before the child is saved and invalid after the roll-up changes it. Suppose a validation rule on Invoice__c says Total_Amount__c must not exceed a credit limit. A user adds a line item that pushes the total over the limit. The error appears on the line item save, even though the user never edited the invoice. Salesforce rolls back the line item along with the failed parent save.
When you debug a child save that fails with an unfamiliar message, check the parent object's validation rules, triggers, and flows.
Parent triggers can re-enter the child
A parent trigger or flow may update child records. That update starts the same cycle again: child triggers, then roll-up recalculation, then the parent save. This can loop. Guard against it with a static set of processed Ids, as described in the recursion chapter.
public class RollupGuard {
public static Set<Id> handled = new Set<Id>();
}
trigger InvoiceAfterRollup on Invoice__c (after update) {
List<Invoice__c> work = new List<Invoice__c>();
for (Invoice__c inv : Trigger.new) {
if (inv.Total_Amount__c != Trigger.oldMap.get(inv.Id).Total_Amount__c
&& !RollupGuard.handled.contains(inv.Id)) {
RollupGuard.handled.add(inv.Id);
work.add(inv);
}
}
// Process only the records in work
}Design guidance
- Put logic that depends on the final roll-up value in the parent's triggers or flows.
- Always compare old and new values so unrelated parent updates do not trigger the logic.
- Bulk-load children in one operation. A data load of many children for one parent recalculates the parent once per transaction batch, not once per child, but the parent's automation still shares your limits.
- Account for a parent failure when testing child inserts. Write tests that push the roll-up past a parent validation threshold.
Exercise
A line item insert fails with a validation error message that mentions a field on the parent invoice, which the user never edited. What is the most likely cause, and why does the line item not stay saved?
Show answer
The insert changed the parent's roll-up summary field. Salesforce then saved the parent inside the same transaction, and a validation rule, trigger, or flow on the parent raised an error. Because the parent save is part of the child's transaction, the error rolls back the whole transaction, including the line item.
Exercise
A child after insert trigger queries the parent's roll-up total and uses it in a calculation. The result is always one save behind. Why, and what are two ways to fix it?
Show answer
The child's after trigger runs before the roll-up is recalculated, so the query returns the old value. Fix it by calculating the total from the child records in the trigger, or by moving the logic to the parent's trigger or flow, where the new roll-up value is available in Trigger.new.