Sub topic 9
Rolling back to a savepoint with Database.rollback
This chapter assumes you can write basic Apex, run DML statements such as insert, and use try/catch with DmlException. You should also know that governor limits cap what one transaction can do.
What Database.rollback does
Database.setSavepoint() marks a point in the current transaction. Database.rollback(sp) restores the database to that point. Every DML change made after the savepoint is undone. Work done before the savepoint stays in the transaction.
Rollback matters because catching an exception does not undo earlier DML. A savepoint lets you undo a group of related changes yourself and then carry on.
Worked example
The code below creates an account and a contact together. If either insert fails, both are removed.
Savepoint sp = Database.setSavepoint();
try {
Account acct = new Account(Name = 'Acme Test');
insert acct;
Contact c = new Contact(LastName = 'Doe', AccountId = acct.Id);
insert c;
} catch (DmlException e) {
Database.rollback(sp);
System.debug('Rolled back: ' + e.getMessage());
}Follow the flow:
- The savepoint is set before any related DML runs.
- Both inserts run inside the
tryblock. - If one throws a
DmlException, thecatchblock callsDatabase.rollback(sp). - The account and contact inserts are undone, and the transaction continues after the
catchblock.
Rules to remember
- The rollback affects only the current transaction. Nothing that was already committed can be undone.
- Records saved before the savepoint are not touched.
- A rollback does not stop your code. Execution continues with the next statement.
- Rolling back counts against the DML statement limit. Setting a savepoint counts too. A later chapter covers the cost.
Savepoints are invalidated by earlier rollbacks
If you roll back to an earlier savepoint, every savepoint set after it becomes invalid. Using one of those savepoints causes a runtime error.
Savepoint sp1 = Database.setSavepoint();
insert new Account(Name = 'A');
Savepoint sp2 = Database.setSavepoint();
insert new Account(Name = 'B');
Database.rollback(sp1);
Database.rollback(sp2); // fails at runtime: sp2 was invalidated by rolling back to sp1The first rollback removes both accounts and discards sp2. The second call throws a runtime error. When you use nested savepoints, roll back to the outermost one you need, and do not reuse the inner ones afterward.
Common errors
| Mistake | Result |
|---|---|
| Setting the savepoint after the DML you want to undo | That DML is not rolled back |
| Calling `Database.rollback` with a savepoint invalidated by an earlier rollback | Runtime error |
| Assuming a rollback resets variables | Only database changes are reverted |
| Inserting a record again after rollback without clearing its Id | The record still holds the old Id |
The last two rows are covered in the next chapters on what a rollback does not revert.
Exercise
A method sets savepoint sp1, inserts an Account, sets savepoint sp2, and inserts a Contact. It then calls Database.rollback(sp1) and afterward Database.rollback(sp2). What happens, and what is left in the database from this transaction?
Show answer
The call to Database.rollback(sp1) undoes both the Account and the Contact. Rolling back to sp1 invalidates sp2, so the later Database.rollback(sp2) fails with a runtime error. Neither record from this transaction remains.