+ Start a Discussion
Vigneshwaran LoganathanVigneshwaran Loganathan 

System.CalloutException: You have uncommitted work pending.

Hi,

I'm facing below error in Callout. Can anyone help?


System.CalloutException: You have uncommitted work pending. Please commit or rollback before calling out
public static void CallforListContactsRequest(string accno)
    {  
       
        contactrequest cntreq1 = new contactrequest();
        contactrequest cntreq = new contactrequest();
        cntreq1.Customername = 'TelAssistant';
        cntreq1.Username = 'Salesforce';
        cntreq1.Password = '&7mXvDGwvU';
        cntreq1.ThirdpartyAccountID= accno;
        //cntreq1.ClientId='517ec5430f4e161a400dc07a';
        String jsonBody = json.serialize(cntreq1);
        system.debug(jsonBody);
        system.debug('this is called from CallforListContactsRequest');
        HttpResponse res;
        HttpRequest req = new HttpRequest();
        req.setEndPoint('https://telassistant.hostedsuite.com/api/json/reply/ListClientsRequest'); 
        req.setMethod('POST');
        req.setHeader('Content-Type','application/json');
        req.setBody(jsonBody);
        Http http_req = new Http();
      try 
        {
            res = http_req.send(req);  //********ERROR  AT THIS LINE  
            system.debug('bodycontact'+res);
           // if(res.getbody()!='[]')
             //{    
                string httpresp = res.getBody();
                String reqresponse = res.getBody();
                list<contactrequestresp> deserializedString= (list<contactrequestresp>)JSON.deserialize(reqresponse,list<contactrequestresp>.class);
                system.debug('deserializedString contact request'+deserializedString);
                for( contactrequestresp d1 : deserializedString )
                  {
                     clientidx = d1.id;
                     system.debug('** client name **' + d1.id );
                     system.debug('** client name **' + d1.clientname );
                     system.debug('**3rdpartyno **' + d1.ThirdPartyAccountId );
                     system.debug('**Client id **' + d1.ClientId );
                 }//}  
          /***   else
             {
                Daily_Billing_Error_Log__c e8 = new Daily_Billing_Error_Log__c();
                e8.Accountno3__c = accno;
                e8.processdate__c = system.today();
                e8.errormsg__c = ' contact request response error '; 
                insert e8;
             }***/          
        }  
        catch(System.CalloutException e) 
        {
            Daily_Billing_Error_Log__c e9 = new Daily_Billing_Error_Log__c();
            e9.Accountno3__c = accxx;
            e9.processdate__c = system.today();
            e9.errormsg__c = ' call out error - contact request '; 
            insert e9;
           
        }
        
     }
Thanks
Sneha1991Sneha1991
Hi,

One of the main reason of this exception is that you are doing some CRUD operation in your code, means you are doing some DML operation and then doing call out in single run.

You have to break thread with @future annotation.

Thanks