function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
SahajSahaj 

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

Hi,
Please help me to fix this error. 
Below is the code
Thanks :)
 
Global class Moogsoft {
    @future (callout=true)
    public static  void getMoog(String inc, Decimal moog,id iid) {
        HttpResponse  res = authToken();
        String authT;
        JSONParser parser = JSON.createParser(res.getBody());
            while (parser.nextToken() != null) {
                if (parser.getCurrentToken() == JSONToken.FIELD_NAME){
                    String fieldName = parser.getText();
                    parser.nextToken();
                     if(fieldName == 'auth_token' ) {
                        authT = parser.getText();
                        system.debug('++++++++++'+authT);
                    }
                }
            }
        system.debug('####'+authT);
        HttpResponse  firstc = firstCall(inc,moog,authT);
        system.debug('####first'+firstc.getbody());
        HttpResponse secondC = secondCall(inc,moog,authT);
        system.debug('####'+secondC);
        HttpResponse thirdC = thirdCall(iid,moog,authT);
        system.debug('####'+thirdC);
        
    }
     public static httpResponse authToken() {
        String URL1 = 'pqr.com';
        HttpResponse resData = HTTPCallout(URL1, 'GET');
        System.debug('Response from Moog: ('+resData.getStatusCode()+')'+resData.getBody());
           if(resData.getStatusCode()>299) {
            String error = 'Failed getting a request token. HTTP Code = '+resData.getStatusCode()+
                            '. Message: '+resData.getStatus()+'. Response Body: '+resData.getBody();
            system.debug('failed'+error);
               MoogError__c me = new MoogError__c();
               me.MoogError1__c = error;
               insert me;
               
               return resdata;
                } 
         
          else {
            return resData;
            }
     }
    public static httpResponse firstCall(String a1,Decimal b1,String c1) {
        String aa = a1;
        Decimal bb = b1;
        String cc = c1;
        System.debug('####'+cc);
        System.debug('####'+aa);
        String URL2 = 'abc.com';
        HttpResponse res = HTTPCallout(URL2, 'POST');
        System.debug('Response from Code request: ('+res.getStatusCode()+')'+res.getBody());

        if(res.getStatusCode()>299) {
            String error = 'Request failed error.HTTP Code = '+res.getStatusCode()+'. Message: '+res.getStatus()+'. Response Body: '+res.getBody()+'auth_token='+cc+'&sitn_id='+bb+'&service_name=Remedyforce&resource_id='+aa+'';
            System.debug('##### Failed: '+error);
            MoogError__c me = new MoogError__c();
            me.MoogError2__c = error;
            insert me;
            system.debug(me);
            system.debug('value of'+me.MoogError2__c);
           return res;
        }
        
        
        return res;
        
        }

    public static httpResponse secondCall(String a2,Decimal b2,String c2) {
        String aa = a2;
        Decimal bb = b2;
        String cc =  c2;
        
        
     String URL3 = 'abc.com;
        
       
        HttpResponse resT = HTTPCallout(URL3, 'POST');
        System.debug('Response from Code request: ('+resT.getStatusCode()+')'+resT.getBody());

        if(resT.getStatusCode()>299) {
            String error = 'Request failed error.HTTP Code = '+resT.getStatusCode()+'. Message: '+resT.getStatus()+'. Response Body: '+resT.getBody()+'auth_token='+cc+'&sitn_id='+bb+'&custom_info=%7B%22incrf%22%3A%22'+aa+'%22%7D';
            System.debug('##### Failed: '+error);
            MoogError__c me = new MoogError__c();
            me.MoogError3__c = error;
            insert me;
            
            
            return resT;
        }
        
        
         system.debug('success');
        return resT;
             
    }
   
    public static httpResponse thirdCall(id a2,Decimal b2,String c2) {
        id aa = a2;
        Decimal bb = b2;
        String cc =  c2;
        System.debug('3rd call');
        
      String URL3 = 'pqr.com';
              HttpResponse resm = HTTPCallout(URL3, 'POST');
        System.debug('Response from Code request: ('+resm.getStatusCode()+')'+resm.getBody());

        if(resm.getStatusCode()>299) {
            String error = 'Request failed error.HTTP Code = '+resm.getStatusCode()+ '. Message: '+resm.getStatus()+'. Response Body: '+resm.getBody()+'auth_token='+cc+'&sitn_id='+bb+'&custom_info=%7B%22incrfer%22%3A%22'+aa+'%22%7D';
            System.debug('##### Failed: '+error);
            MoogError__c me = new MoogError__c();
           me.MoogError4__c = error;
            insert me;
           
            return resm ;
        }
        

   
        system.debug('sucessfull');
        return resm;
    }

    public Static HttpResponse HTTPCallout(String EndPoint, String Method) {
        Http h = new Http();
        HttpRequest req= new HttpRequest();
        req.setEndpoint(EndPoint);
        req.setMethod(Method);
        HttpResponse res = null;
        res = h.send(req);
        return res;
    }
}

 
sharathchandra thukkanisharathchandra thukkani
Since you are doing asynchronus callout you are facing this issue. Remove  @future (callout=true) and try.

Also Web Service Callout may not occur after a DML statement within the same transaction. To achieve the required action, the transaction must be separated into two parts so that the DML transaction is completed before the Web Service Callout occurs.

refer below link 

https://help.salesforce.com/apex/HTViewSolution?id=000003701