Lightning SFDC

Spring 24 – Make Callouts After Rolling Back DML

As part of spring 24 release in apex, Roll back all uncommitted DML by using a savepoint. Then use the new Database.releaseSavepoint method to explicitly
release savepoints before making a desired callout. Previously, callouts after creating savepoints resulted in a CalloutException regardless of whether there was uncommitted DML or the changes were rolled back to a savepoint.

In this example, the makeACallout() callout succeeds because the uncommitted DML is rolled back and the savepoint is released.

Savepoint sp = Database.setSavepoint();
try {
// Try a database operation
insert new Account(name='Foo');
integer bang = 1 / 0;
} catch (Exception ex) {
Database.rollback(sp);
Database.releaseSavepoint(sp);
makeACallout();
}

In this example, the savepoint isn’t released before making the callout. The CalloutException informs you that you must release all active savepoints before making the callout.

Savepoint sp = Database.setSavepoint();
try {
makeACallout();
} catch (System.CalloutException ex) {
Assert.isTrue(ex.getMessage().contains('All active Savepoints must be released before
making callouts.'));
}

When Database.releaseSavepoint() is called, SAVEPOINT_RELEASE is logged if savepoints are found and released.

Leave a Reply