Is there a way to do all that

  1. Do Callout in Queueable
  2. Do Dml based on the Callout result
  3. Catch errors in 1 and 2.
  4. Rollback 1, 2 on error
  5. Report error via email and log in Custom object

without running into

common.apex.runtime.impl.ExecutionException: You have uncommitted work pending. Please commit or rollback before calling out

This is how my code looks like:

public class MyQueue implements Queueable, Database.AllowsCallouts {

    public void execute(QueueableContext context) {
        Savepoint sp = Database.setSavepoint();

        try {
            List<SObject> result = getDataViaCallout();


            insert result; 
        catch(Exception ex) {
            insert new ErrorLog__c(Cause__c = ex.getMessage());


I think that the instruction

Savepoint sp = Database.setSavepoint();

makes a DML, then you can't do a callout after that. You just need to do your callout in a separate context.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.