Monday, 1 January 2024

Make Callout After DML Rollback and Releasing Savepoint (Spring 24 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.


To execute callouts post-rollback, adhere to these steps:


  1. Revert all uncommitted changes using a savepoint.
  2. Please make sure to explicitly release the savepoint via the Database.releaseSavepoint method.
  3. Now, perform your intended callout without encountering any hindrances.


Ex :

Savepoint mySavepoint = Database.setSavepoint();


try {

    // Attempt a database operation

    insert new Account(name='Foo');

    Integer divisor = 1 / 0;

} catch (Exception e) {

    // Roll back to the savepoint in case of an exception

    Database.rollback(mySavepoint);

    Database.releaseSavepoint(mySavepoint); // Release any savepoints created after 'mySavepoint'

    

    // Perform a callout as uncommitted work is rolled back and savepoints are released

    initiateCallout();

}

No comments:

Post a Comment