Sub topic 12
Rolling back with savepoints
This chapter assumes you know basic Apex syntax, SOQL and DML, and the idea that a transaction is one unit of work with governor limits. You should also know how try, catch, and DmlException work from earlier chapters.
Why rollback matters
Apex runs inside a transaction. If an exception is thrown and never caught, Salesforce rolls back every change made in that transaction. If you catch the exception, the transaction continues. Any DML that already succeeded stays pending and will be committed when the transaction ends normally.
That is a common trap. Code that inserts a parent record, fails on the child, and swallows the error leaves an orphaned parent behind.
Savepoints and Database.rollback
A savepoint marks a point in the transaction. Calling Database.rollback with that savepoint undoes all DML performed after it.
Savepoint sp = Database.setSavepoint();
try {
Account acc = new Account(Name = 'Parent');
insert acc;
insert new Contact(AccountId = acc.Id);
} catch (DmlException e) {
Database.rollback(sp);
throw e;
}The Contact has no LastName, so its insert throws a DmlException. The catch block rolls back to the savepoint, which removes the Account. The throw e then tells the caller the operation failed.
What a rollback does
- It reverses DML done after the savepoint, including inserts, updates, deletes, and undeletes.
- Records inserted and then rolled back lose their Ids. The
acc.Idvalue in the variable is still set, but it no longer refers to a saved record. Do not reuse that sObject for a later insert without clearing the Id. - It does not reset governor limits. Each DML statement and row counted before the rollback still counts.
- It does not undo callouts, sent emails, or other external effects.
Savepoint rules and limits
- Each
setSavepointcall and eachrollbackcall counts against the DML statement limit. - A savepoint can be used only within the same transaction that created it.
- If you roll back to an earlier savepoint, any savepoint created after it becomes invalid. Using one throws a
System.InvalidParameterValueException. - Setting a savepoint and then performing a callout before the rollback is allowed, but the callout is not undone.
Choosing between rethrow and handling
After a rollback you have two choices.
| Choice | Result | Use when |
|---|---|---|
| Rethrow the exception | The caller sees the failure and the transaction ends in error | The caller must know the work failed |
| Return a result object | The caller continues with a clear status | You are processing independent units and want to report each one |
Rolling back and then silently returning is rarely correct, because the caller will assume the work succeeded.
Combining with partial success
With Database.insert(records, false), valid records save even if others fail. That is the right tool when records are independent. Use a savepoint when records depend on each other and all must succeed or none should be saved.
Exercise
A method inserts an Account, then inserts a Contact that fails with a DmlException. The catch block logs the error and returns without rolling back or rethrowing. Is the Account saved when the transaction ends? Why?
Show answer
Yes. Catching the exception lets the transaction continue, so the Account insert is still pending and is committed at the end. To avoid this, call Database.rollback with a savepoint taken before the first insert, then rethrow or report the failure.