Is it possible to implement transactional DML for batches?
for example we have parent object and child and if the update of child object is failed then the update of this particular parnet and child should be rolled back but the rest parent-child objects should be saved if they are updated without errors. But the difficulty is that there are chunks of such records: 10000 parents and each parent have 2 child. Is it possible to leverage System.Savepoint in clever way to support batch update
[SalesForce] Transaction Control for multiple batches
Related Topic
- [SalesForce] Can a DML retry solve UNABLE_TO_LOCK_ROW race conditions
- [SalesForce] How to handle error records in Batch apex
- [SalesForce] Batch Apex Query – when multiple batch jobs fails
- [SalesForce] Batch Apex that does DML and multiple callouts
- [SalesForce] Calling another batch from the execute method of another batch
Best Answer
When you perform an update, and that update performs additional updates via a trigger, you can partially fail the transaction to perform an automatic rollback and partial retry.
Consider the following trigger:
If you perform an update of accounts in an execute function, like this:
What's not obvious here is that the system automatically performs a rollback on all records that error out, and attempt to retry the accounts that are left. This is an implied partial callback. Nowhere am I using a SavePoint to rollback the errors; the system handles the logic for me. The child records that failed also roll back, so the contacts that were on the accounts that failed would also not update as a result of this code.