【问题标题】:Salesforce Webservice ErrorSalesforce 网络服务错误
【发布时间】:2012-05-10 12:54:13
【问题描述】:

我从调用 Web 服务的类中收到以下错误。

"You have uncommitte​d work pending. Please commit or rollback before calling out"

这是调用 web 服务的类:

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;   
     }          
}   
}

更新:这是我正在执行 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 - 这里是 applyURLString() 被执行的地方。这是在我的 HTTP 请求之前执行 DML 的唯一位置。但我需要新 Builder 记录的 ID。

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);

任何想法为什么会发生此错误?

【问题讨论】:

  • 我们需要查看调用此类的类/触发器 - 如果您在更新数据库后进行调用,您将收到此消息。
  • 好的,我在上面添加了执行代码。
  • 再次更新(#2)我认为是导致问题的代码。
  • 在调用 applyURLString 之前,您有一个 DML 语句(插入 insertBuild;)。就像我之前说的,如果您在同一事务中更新数据库后尝试进行标注,您将收到此消息。我可能会建议将 applyURLString 方法作为未来的标注来解决这个问题。
  • @future 成功了。谢谢!

标签: web-services soap salesforce apex-code


【解决方案1】:

@JCD 使用@future 注释的建议解决了我的问题。再次感谢!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多