Sub topic 13
When roll-ups recalculate and what gets locked
This chapter assumes you can create a roll-up summary field on a master-detail relationship and write basic Apex or use Data Loader. It covers what triggers a recalculation, what doesn't, and why bulk loads sometimes fail with row-lock errors.
Roll-ups are stored values
A formula field is computed each time you read it. A roll-up summary field is different. Salesforce stores its value on the master record and updates it when something about the detail records changes. This has two consequences:
- The stored value is only as fresh as the last triggering event.
- Updating that stored value is a write to the master record, so it takes a lock.
What triggers a recalculation
Salesforce recalculates the roll-up on the master record when any of these happen to a detail record:
| Event on the detail record | Effect on the roll-up |
|---|---|
| Insert | The new record is included if it meets the filter criteria. |
| Update to the summarized field | The total, minimum or maximum is recomputed. |
| Update to a field used in the filter criteria | The record enters or leaves the roll-up. |
| Delete | The record is removed from the roll-up. |
| Undelete | The record is added back. |
| Reparenting (changing the master-detail field) | Both the old and new master are recalculated. |
Reparenting only works if the master-detail field has "Allow reparenting" enabled. Records in the Recycle Bin are not counted.
A change to a field that the roll-up doesn't use does not change the result.
What does not recalculate automatically
Editing the roll-up field's definition does not refresh existing values. If you change the filter criteria, the summary type, or the field being totaled, the master records keep their old numbers until you run a mass recalculation from the roll-up field's detail page in Setup. The recalculation runs as a background job, so large objects may take a while.
A newly created roll-up field is calculated for existing records when you save it. Don't confuse this with the edit case.
What happens inside a transaction
When you save a detail record, Salesforce updates the master as part of the same save. Per the Apex order of execution, this happens after the detail record's own after-save work. The master record then goes through its own save procedure, so its triggers and flows can run. A failure there surfaces as an error on the detail save.
In Apex, the master's in-memory copy is not updated. You must re-query it to see the new value, as with formula fields.
Invoice__c inv = new Invoice__c(Name = 'INV-002');
insert inv;
insert new Line_Item__c(Invoice__c = inv.Id, Amount__c = 250, Is_Billable__c = true);
Invoice__c fresh = [SELECT Total_Amount__c FROM Invoice__c WHERE Id = :inv.Id];
System.debug(fresh.Total_Amount__c); // 250What gets locked
While the roll-up is recalculated, Salesforce locks the master record. If the master is itself a detail in another master-detail relationship that has its own roll-up, the chain continues, and the next master up is locked too.
Locks are held until the transaction finishes. Another transaction that needs the same master record waits briefly. If it can't get the lock in time, it fails with UNABLE_TO_LOCK_ROW.
This matters most when many detail records for the same master are saved at once, for example:
- A parallel Bulk API load where different batches each contain line items for the same invoice.
- Several integrations writing payments for one account at the same moment.
- A trigger on the detail object that also updates the master, so the lock is taken twice.
A single user editing one line item will rarely notice anything. Problems show up under concurrency.
Reducing lock contention
- Sort the load by master Id. Records for the same master then land in the same batch instead of being spread across parallel batches.
- Use serial mode in the Bulk API when contention can't be avoided. It is slower but removes competing batches.
- Use smaller batches so each transaction holds its locks for less time.
- Retry on lock errors. Treat
UNABLE_TO_LOCK_ROWas transient and retry the failed records. - Avoid extra master updates in detail triggers. The roll-up already writes to the master.
- Avoid very wide master records with thousands of children being edited in parallel. If that is unavoidable, consider whether a roll-up is the right tool. See the chapter on alternatives.
Troubleshooting stale or surprising values
| Symptom | Likely cause | What to do |
|---|---|---|
| Total doesn't match after changing the filter | Definition edited, no recalculation | Run the mass recalculation. |
| Record seems missing from the total | It fails the filter criteria, or it is in the Recycle Bin | Check the filter fields on that record. |
| Apex reads an old value after DML | The in-memory master was not refreshed | Re-query the master. |
| `UNABLE_TO_LOCK_ROW` during a load | Parallel batches touching the same master | Sort by master Id, use serial mode, or use smaller batches. |
| Detail save fails with a master-side error | A trigger or flow on the master failed during the roll-up update | Debug the master's automation. |
Key points
- A roll-up is stored and updated by events on detail records. It is not computed on read.
- Changing the roll-up definition doesn't update existing values, so run a mass recalculation.
- Every recalculation locks the master record until the transaction ends.
- Contention comes from concurrency, so fix it by shaping the load, not by changing the field.
Exercise
You change the filter criteria of a SUM roll-up from "IsBillablec = True" to "IsBillable_c = True AND Status_c = 'Approved'". Afterwards, a report still shows the old totals on existing invoices. Why, and what do you do?
Show answer
Editing a roll-up's definition does not recalculate stored values. Existing invoices keep the totals computed under the old criteria until a detail record changes or you run a mass recalculation. Run the mass recalculation for that roll-up field from its detail page in Setup, then check the report again.
Exercise
A Bulk API load of 200,000 line items runs in parallel and many batches fail with UNABLETOLOCK_ROW. Name two changes that reduce the failures, and say why they work.
Show answer
Sort the file by the master (invoice) Id so line items for the same invoice are in the same batch, and run the job in serial mode. Each insert locks the master invoice while its roll-up is recalculated. Parallel batches that include the same invoice compete for that lock. Sorting keeps those records together, and serial mode removes the competition. Smaller batches and retrying failed rows also help.