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

System.CalloutException: You have uncommitted work pending.

Hi all,

    I'm getting this error whenever I'm trying to save......After a research I came to know that  you cannot perform DML (insert, update, delete) prior to a callout ( http.send() )......I'm a newbie to the apex code......what should I do in my code to get rid of this.....Any help please


  public string main(string url)
            Http h = new Http();    
            HttpRequest req = new HttpRequest();
            string result='';
                HttpResponse res = h.send(req);
                result = res.getBody();
                result = '<?xml version="1.0" encoding="ISO-8859-1"?><Widgets><BaseURL></BaseURL><Category FolderName="GoogleMaps" Name="GoogleMaps" Image=""><Widget Name="Facebook"><FieldLabel FieldType="Textbox" image="">FacebookURL</FieldLabel></Widget></Image></Category></Widgets>';
            return result;


public void Save()
     widgetpro = new C_P_Widgets__c();
     widgetpro.Widget_type__c = layoutname;
     widgetpro.Widget_Category__c = Name1;   
      insert widgetpro;



Assuming you can't restructure your code to do the callout before the DML, the solution is to carry out the callout asynchronously.  One way to do this is to create an @future method:


another would be to run a scheduled job that handles the callouts.  There's an example at:

Rakesh Aggarwal.ax1406Rakesh Aggarwal.ax1406

You may want to split your DML operation and HTTPCallout into 2 functions. On click of your save button in VF Page, call apex code functions using actionFunction.. I had explained similar approach to avoid some governor limits. Let me know if this works or you need additional details.


Rakesh Aggarwal