1

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) {
            sendEmail(ex);
            insert new ErrorLog__c(Cause__c = ex.getMessage());

            Database.rollback(sp);
        }
    }
}

1 Answer 1

1

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.

2

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .