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
dujsudujsu 

Webservice Error: You have uncommitted work pending. Please commit or rollback before calling out

 

Hello,

 

Im all of a sudden getting the following error from my class that invokes a webservice:

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

 

Webservice Class

 

global class myWS   
{   
   
   WebService static string invokeExternalWs(string childValue, string parentValue)   
    {   
        HttpRequest req = new HttpRequest();   
        req.setEndpoint('https://externalURL/Services');   
        req.setMethod('POST');   
        req.setHeader('Content-Type', 'text/xml; charset=utf-8');   
        req.setHeader('SOAPAction', 'http://externalService/externalMethod');  
        string b = '--soap request goes here--';   
        req.setBody(b);   
        Http http = new Http();   
        try {   
          //Execute web service call here 
          String xObjectID ='';   
          HTTPResponse res = http.send(req);
          Dom.Document doc = res.getBodyDocument();
          String soapNS = 'http://schemas.xmlsoap.org/soap/envelope/'; 
          Dom.XmlNode root = doc.getRootElement();
          for(dom.XmlNode node1 : root.getChildElements()) {
               for(dom.XmlNode node2 : node1.getChildElements()) {
                   for(dom.XmlNode node3 : node2.getChildElements()) {
                      for(dom.XmlNode node4 : node3.getChildElements()) {
                          xObjectID = node4.getText();
                       }
                   }
               }
             }
     
         return xObjectID;
       } catch(System.CalloutException e){   
          return 'ERROR:' + e;   
     }          
}   
}

 UPDATE: Here is my class that is executing myWS

public void applyURLString(ID ArgBuildID) {

    Builder__c current_build = [SELECT id, name, LLURL__c, column1, column2, Opportunity__c
                FROM Builder__c
                WHERE id = :ArgBuildID];

        if(current_build.LLURL__c == null || current_build.LLURL__c.trim().length() == 0)
            {

                String tmpFolderName = current_build.column1 + ' - ' + current_build.column2;

                String LLWSResultPattern = '[0-9]{2,}';

                String myWSXMLResult = myWS.invokeExternalWs(tmpFolderName,'test');

                Boolean LLWSPatternMatched = pattern.matches(LLWSResultPattern,myWSXMLResult);
                if(LLWSPatternMatched)
                {
                    Opportunity oppt = [SELECT Id,Name 
                                        FROM Opportunity 
                                        WHERE Id = :current_build.Opportunity__c
                                        LIMIT 1];   
                    oppt.LLURL__c = 'https://someService/' + myWSXMLResult;
                    update oppt;
                } 
            } 
    }

 

 

UPDATE #2 - Here is the code that executes applyURLString()

 

 

Builder__c insertBuild = new Builder__c();      
insertBuild.Opportunity__c = opportunityId;
insertBuild.Product_Group__c = selectedBuild.Product_Group__c;
insertBuild.Manual_Build_Product__c = selectedBuild.Manual_Build_Product__c;

insert insertBuild;

applyURLString(insertBuild.Id);

 

 

 

Thank you for your time!

dujsudujsu

Ive updated my original post with additional code. See "UPDATE".

dujsudujsu

Just wanted to pass along the solution that was suggested to me on stackoverflow. An @future annotation was added to the applyURLString() method. For my specific case it has worked perfectly.

 

@future (callout=true)
public static void applyURLString(ID ArgBuildID) {
...

 Hope this helps someone in the future.