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 know the basic idea of a database transaction. It covers what makes a roll-up recalculate and why that can cause row-lock errors.
What triggers a recalculation
A roll-up summary field is stored on the master record. Salesforce updates it when something changes in the child records. Recalculation happens when:
- A child record is inserted, deleted, or undeleted.
- A child record is updated and the change affects the result. That means the summed field changed, a field used in the filter criteria changed, or the child moved to another master.
- A child is reparented. Both the old master and the new master are recalculated.
Changes to unrelated child fields do not matter. Updating a Description__c field on a line item leaves Total_Amount__c alone.
If you edit the roll-up definition, for example by changing the filter criteria, existing values are not guaranteed to be correct right away. Open the field detail page and use Force a mass recalculation of this field.
Why the master record gets locked
Each recalculation writes to the master record. To keep the result consistent, Salesforce locks the master while a child save is in progress. Other transactions that need to change the same master must wait. If the wait is too long, they fail with UNABLE_TO_LOCK_ROW.
Where it goes wrong
Lock errors usually appear in bulk work: data loads, integrations, or parallel batch jobs. Many child records for the same master are processed in different threads at once. A few thousand line items for one invoice will collide.
Ways to reduce it:
- Sort the load file by master record Id so one batch holds all children of a given master.
- Use serial mode in Bulk API so batches do not run in parallel.
- Use smaller batch sizes when many children share one master.
- Avoid very large numbers of children under a single master. Split the data model if you can.
Reading the result in code
The new roll-up value is not written to the in-memory master record in your code. Query the master again after the DML on the child.
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 refreshed = [SELECT Id, Total_Amount__c FROM Invoice__c WHERE Id = :inv.Id];
System.debug(refreshed.Total_Amount__c);Exercise
A nightly integration loads 200,000 line items in parallel batches. Many fail with UNABLETOLOCKROW on the parent Invoice_c. Give two changes that reduce these errors, and explain why they work.
Show answer
Sort the file by Invoice__c Id so each invoice's children are in the same batch, and run the Bulk API job in serial mode. Each child save locks its master during roll-up recalculation. Parallel batches that hold children of the same invoice compete for that lock. Grouping by parent and processing batches one at a time removes the competition.