0

I am getting

System.CalloutException: You have uncommitted work pending. Please commit or rollback before calling out.

global class BatchClassName implements Database.Batchable<`string`>,
                                       Database.Stateful, 
                                       Database.AllowsCallouts {

    //Local Variables
    global list<string> lstTeamAssignedOfficerId;
    global final list<string> lstAccountOfficerId;

    global <BatchClassName_Constructor>(list<string> lstAccountOfficer) {
        this.lstAccountOfficerId = new list<string>();
        this.lstAccountOfficerId = lstAccountOfficer;
    }

    global Iterable<string> start(Database.BatchableContext ctx) {
        return lstAccountOfficerId;
    }

    global void execute(Database.BatchableContext ctx, list<string> lstAccountOfficer) {
        /*Performing All SOQL and DML operations - No Callouts performed*/
    }

    global void finish(Database.BatchableContext ctx) {
        /*Performing Callout */        
    }
}

I think Finish method will execute after the all the Batches are completed and data is commit in objects. I have written code in past to perform callout in Finish Method and DML operations in execute method.

  • I believe since you are implementing database.stateful that the DML persits and thus you cannot make a callout in the finish method – Eric Aug 5 '15 at 14:38
  • @Eric - According to Salesforce documentation Database.Stateful is to hold information between multiple Batch execute method transactions or between batch method execute and finish. I already developed a Batch class implementing Stateful, AllowCallouts. APEX callout was in finish method and batch class is not throwing any error while processing. I am just curious is Finish method is a different transaction and called after execute finish all the DML operations. – ArpitRao Aug 5 '15 at 15:23
3

I Identified the issue in Batch Class code. There was a DML operation between callouts which was causing this.

Thanks for your help @Eric.

believe since you are implementing database.stateful that the DML persits and thus you cannot make a callout in the finish method

Database.stateful is to persist values of instance variables and not for DML operations. Issue was in Finish method code, while doing 4 difffenent APEX callouts code was inserting records(DML) inbetween callouts. I just moved all the DML statements after callouts to resolve the issue.

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.