Skip to content

Instantly share code, notes, and snippets.

@msrivastav13
Last active May 11, 2026 14:01
Show Gist options
  • Select an option

  • Save msrivastav13/67a369ae1508f6160ba47248cb7abc52 to your computer and use it in GitHub Desktop.

Select an option

Save msrivastav13/67a369ae1508f6160ba47248cb7abc52 to your computer and use it in GitHub Desktop.
Example to show the callout after DML rollback in Apex with new Database.releaseSavepoint
// This class works if your class -meta.xml is of 60.0 version
public with sharing class SavePointReleaseCalloutExample {
public void basicAuthCallout() {
// Do a DML before callout
Savepoint sp = Database.setSavepoint();
Account acc = new Account();
acc.Name = 'Test';
insert acc;
Database.rollback(sp);
Database.releaseSavepoint(sp);
HttpRequest request = new HttpRequest();
request.setEndpoint('https://dog.ceo/api/breeds/image/random'); // Set the endpoint URL
request.setMethod('GET'); // Set the HTTP POST method
// Set the request headers
request.setHeader('Content-Type', 'application/json');
request.setHeader('Accept', 'application/json');
// Send the request
Http http = new Http();
HttpResponse response = http.send(request);
// Process the response
if (response.getStatusCode() == 200) {
String responseBody = response.getBody();
System.debug(responseBody);
}
}
}
@LoganTann-pro

Copy link
Copy Markdown

Please note that if the Account Trigger does a DML-ish operation (eg. sending a mail or scheduling a batch), then the Http request will still throw a CalloutException even if the transaction is rollbacked and Database.releaseSavepoint(sp); is called.

About the dml-ish thing (made-up term) : https://salesforce.stackexchange.com/a/240041

Some operations are "DML-ish", meaning they persist something to the database to be committed at the end of the transaction, just not a standard DML operation on an sObject. Enqueuing Batch Apex, for example, is a DML-ish operation. [...] Because this is persisting something (the email send attempt) until the transaction commits, it has the same effect on later callouts as regular DML - that is, it blocks them due to the uncommitted work that's in flight.

About releaseSavepoint being uneffective : https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_transaction_control.htm

If there’s uncommitted work pending when Database.releaseSavepoint() is called, the uncommitted work isn’t rolled back. It’s committed if the transaction succeeds.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment