Sub topic 12
What a Rollback Does Not Revert
This chapter assumes you can run insert and update statements, use try/catch, and have used Database.setSavepoint and Database.rollback. A rollback undoes database changes made after the savepoint. It does not undo anything that lives in memory or outside the database.
What the rollback reverts
Database.rollback(sp) discards the DML changes made since the savepoint was set. It does not touch the Apex heap, which is where your variables live. Anything your code holds in memory stays as it was.
The Id stays on the sObject
When you insert a record, Apex writes the new Id onto your in-memory sObject. A rollback removes the row, but the Id value stays on the variable. The record now points to a row that does not exist.
If you try to insert that same variable again, the platform sees an Id and rejects the insert. Clear the Id first.
Savepoint sp = Database.setSavepoint();
Account a = new Account(Name = 'Temp');
insert a;
Database.rollback(sp);
System.debug(a.Id); // the Id value is still set on the in-memory record
a.Id = null; // clear it before inserting this record again
insert a;An update on the stale record fails in a different way. It raises an error because the row it refers to is gone.
Static variables and other in-memory state
Static variables, instance fields, and collections keep any changes you made after the savepoint. Suppose a static counter tracks how many records you processed. If you roll back, the counter still shows the higher number.
public class RollbackState {
public static Integer processed = 0;
public static void run() {
Savepoint sp = Database.setSavepoint();
insert new Account(Name = 'Counted');
processed++;
Database.rollback(sp);
System.debug(processed); // 1, even though the insert was undone
}
}This matters most for static flags used to stop trigger recursion. If a trigger sets a "has run" flag and a rollback follows, the flag stays set. Later code in the same transaction may then skip work it should do. Reset such flags yourself after a rollback.
Work outside the database
A rollback cannot recall work that has already left the transaction. Examples include:
- Emails sent with
Messaging.sendEmail. Some email work is deferred to the end of the transaction, but do not rely on a rollback to cancel it. - Callouts to external systems that already completed.
- Platform events published with
EventBus.publish, depending on the publish behavior set on the event.
Keep these actions until after the database work has succeeded. That way a rollback is never needed to undo them.
Summary of what stays
| Item | Reverted by rollback? |
|---|---|
| Inserted, updated, and deleted rows | Yes |
| Id on an in-memory sObject | No |
| Static variables and instance fields | No |
| Completed callouts | No |
Exercise
A trigger sets a static Boolean alreadyRan = true to prevent recursion. Later in the same transaction your code rolls back to a savepoint set before the trigger ran, then repeats the DML. Why might the trigger skip its work the second time, and what should you do?
Show answer
The rollback reverted the database rows but not the static variable, so alreadyRan is still true and the trigger believes it has already run. Reset the flag to false after the rollback, before repeating the DML.