Sub topic 8
Setting a Savepoint
This chapter assumes you can write basic Apex, run DML such as insert and update, and use try/catch with DmlException. You should also know that governor limits cap what one transaction can do.
Why savepoints exist
An earlier chapter showed that catching an exception does not undo DML that already succeeded. If you want to keep running after a failure but also discard the partial work, you need a way to mark a point in the transaction and return to it later. A savepoint is that marker.
A savepoint is a reference to the state of the database at one moment in the current transaction. You create it with Database.setSavepoint(), which returns a Savepoint object. Nothing is saved to disk. The object only records where you are, so you can later ask the database to go back to it.
Creating a savepoint
Set the savepoint before the DML you might want to undo. Work done before the savepoint stays outside the scope of any later rollback to it.
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());
}Three things happen here:
Database.setSavepoint()records the current state.- Both inserts run inside the
tryblock. If either throws aDmlException, control moves tocatch. Database.rollback(sp)undoes both inserts, so you do not end up with an Account that has no Contact.
The next chapter covers Database.rollback in detail. Here the focus is on where and how you set the savepoint.
Where to place it
Place the call immediately before the group of changes that must succeed or fail together. Setting it too early makes the rollback cover more work than you intended. Setting it inside the try block, after some DML has run, leaves that earlier DML outside the rollback.
Declare the variable outside the try block. If you declare it inside, the catch block cannot see it, and the code will not compile.
Savepoint sp = Database.setSavepoint(); // visible to both try and catch
try {
// related DML here
} catch (DmlException e) {
Database.rollback(sp);
}Savepoints count against limits
Each call to Database.setSavepoint() counts as a DML statement against the governor limit for the transaction. A rollback counts as well. The limit is 150 DML statements per synchronous transaction, so setting a savepoint inside a loop can use up the budget quickly. Set one savepoint per logical unit of work, not one per record, unless you have a clear reason.
A transaction also has a cap on how many savepoints it can hold. Later chapters cover the exact cost and the cases where an older savepoint becomes unusable.
Exercise
Why must the Savepoint variable be declared before the try block when the catch block calls Database.rollback?
Show answer
Variables declared inside a try block are not in scope in the catch block. Declaring the savepoint before the try makes it visible to both, so the catch block can pass it to Database.rollback.
Exercise
A loop processes 200 records and calls Database.setSavepoint() on every iteration. What problem does this cause?
Show answer
Each savepoint counts as a DML statement. With 200 iterations the loop alone exceeds the 150 DML statement limit and throws a limit exception. Set one savepoint before the loop, or per logical group of work.