+ Start a Discussion
Sushma  RamakrishnanSushma Ramakrishnan 

Apex Batch Job :You have uncommitted work pending. Please commit or rollback before calling out

Hi All,

I have an Apex Class and have written a Batch Apex and Scheduler Apex Class for it.
When I am trying to run my Batch Job from Developer Console I am getting the below error :

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

Since I am a Newbie to Apex Batch Jobs please advise me on how to fix this issue?

Thanks for Any & All Help in Advance...!
karthikeyan perumalkarthikeyan perumal
Hello, 

Work Around for this.. 

This workaround splits the transaction into two separate Ajax processes. The first inserts the record and the second performs the callout and is able to update the newly inserted record.

if its not working kinldy use @future  Annotation  before callout. 

Reference from: https://help.salesforce.com/articleView?id=000003701&type=1

https://help.salesforce.com/articleView?id=000079772&language=en_US&type=1


Hope this will solve your issue. 

Mark best ASNWER if its works for you. 

Thanks
karthik
 
Hemant_SoniHemant_Soni
Hi Sushma Ramakrishnan,

First you have to abort all schedule job related to your batch job or apex class.then again try to execute your batch.
These are the steps to abort scheduled jobs 
User-added image
User-added image

if this answer helps you then make it best answer.
Sushma  RamakrishnanSushma Ramakrishnan
@karthikeyan perumal & Hemant Soni 21 : Thanks so much for the Responses.

I have a single Method which i am calling from my Batch Apex Class->Inside that metthod there are methods which call Webservice callouts.So should i seperate them?

There are no Scheduled Jobs running.

 
Sushma  RamakrishnanSushma Ramakrishnan
Below is the Code for the method Called from my Batch Apex Class :
I have commented the Method Calls which uses Webservice Callouts.
Please let me know on how can I seperate it which will avoid the above error.
public void updateTest()
    {
       String SessionKey = getSessionId();  //(Webservice Callout)
        do {
            Dom.Document docRequest = BuildRequest(); 
            string TZSearchModule = '/module/Room/search';
            
            if(numberOfrequests == 0)
            {   offsetValue = offsetValue + 11;
            }
            else
            {
                offsetValue = offsetValue + 10;
             }

            Dom.Document docResponse = (Dom.Document)this.MakeProcessRequest('POST', TZSearchModule, docRequest, sessionKey);  //(Webservice Callout)
            
            

        } while(totalTest == (numberOfrequests * 10));
        
        upsertOperation();        
    }
Thanks...!!!
 
BALAJI CHBALAJI CH
Hi Sushma,

Trying calling this method with Future annotation. Please find below modified code:
 
@future(callout=true)
public static void updateTest()
{
    String SessionKey = getSessionId();  //(Webservice Callout)
    do {
        Dom.Document docRequest = BuildRequest(); 
        string TZSearchModule = '/module/Room/search';
        
        if(numberOfrequests == 0)
        {   offsetValue = offsetValue + 11;
        }
        else
        {
            offsetValue = offsetValue + 10;
        }
        Dom.Document docResponse = (Dom.Document)this.MakeProcessRequest('POST', TZSearchModule, docRequest, sessionKey);  //(Webservice Callout)
        
    } while(totalTest == (numberOfrequests * 10));
    upsertOperation();        
}

Let us know if that helps you.

Best regards,
BALAJI