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
SFDC pvSFDC pv 

I'm getting "You have uncommitted work pending. Please commit or rollback before calling out" error. I perform the below steps Callout DML Callout- Here am receiving that error . How do i resolve this.

ayu sharma devayu sharma dev
 A 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.
Here (https://help.salesforce.com/articleView?id=000003701&type=1
on this page it is shown that how you can Split your process and make it work. Hope it will work.
SFDC pvSFDC pv
Hi @Ayush,

I haven't use VF Page to call the web service. I/m using controller with future method and trigger.

If users add the values at a time record is getting inserted succesfully without "You have uncommitted work pending. Please commit or rollback before calling out" error. When adding/updating values to the existing values am getting this error. See my below snippet

Class:-

global class QL_xxController {
    
    @future(callout=true)
    Public static void invokeWebService(String CaseId) {
    List<xxx_c > listofxx = new List<xxx_c >();
        
        Case Cas= [SELECT Id,AccountID,Number__c FROM Case  WHERE ID =:CaseId ];
        DELETE [SELECT Id FROM  Frequent_Flyer_Information__c WHERE Case__c =:CaseId limit 20];
if(Cas.Number__c !=null) {
            for (String num : Cas.Number__c .split(',') )
            {             
                if(num!='') {
               Wrapperclass xyzresponse= Invokeapi.invokecustAPI(num);   //  (Here am getting the response)
                if(xyzresponse!= null) {
                         xxx_c ss = new xxx_c;
                         ss.name = xyzresponse.name;
                         ss.number = xyzresponse.number;
                         listofxx .add(ss);
             }
       }
}
            if(listofxx .size()>0) {
                insert listofxx ;
            }
     }
}

How do i split the transaction here. Please help me out

 
Maharajan CMaharajan C
Hi,


You are having the Callout method and Dml Operation in the same transaction.

So if you have the Callout method and Dml Operation in the same transaction then first process the Callout based on the sucess thereafter make the DML operation.

Simply use the Future Method @Future(CallOut = true) to make the callout

OR

================
You will get the Error if the transaction is like after record insert/update

1.  Insert/Update or DML Operation 
2.  callout .

===============

To avoid the error ur transaction should be like below

1.  callout .
2.  Insert/Update or DML Operation 

====================

OR


If you are using visualforce page and the apex:commond button then in the action u can call the DML method and use the ONCOMPLETE function to make the callout.

Can you please Let me know if it helps or not!!!

If it helps don't forget to mark this as a best answer!!!


Thanks,
Raj