[SalesForce] Salesforce Callout Exception

I created a Salesforce client using "Generate class from WSDL" feature. When i try to call the web service, throws the following error:

"System.CalloutException: Web service callout failed: Unexpected
element. Parser was expecting element
'http://schemas.xmlsoap.org/soap/envelope/:Envelope' but found
':html'"

  • The request parameter is a String
  • Endpoint: https://www.consorciotransaccional.cl/WSCrm2/CasoPeoplesoftCRMWSService
  • Method to call web service:

    public PageReference LlamarWS()
    {
      crmWsproviderServiceCnsvCl.CasoPeoplesoftCRMWSPort caso =  new  crmWsproviderServiceCnsvCl.CasoPeoplesoftCRMWSPort();
      XMLResponse = caso.saveCasoPeoplesoftCMRPS(Header);
      return null;
    }
    
  • Variable "Header" is a String containing an XML.

Best Answer

The Generate Class from WSDL tool from Salesforce doesn't always fully create a working Apex Class for you to connect to your Web Service with. Personally, I almost always have to fix the generated Apex Class (from the WSDL).

To deduce what variables you need for your classes, you need to see the actual XML response from your Web Service. I suggest using a program like SoapUI to see these request/responses.

Doing this will help you troubleshoot your error -- the error message suggests to me that you are receiving more parameters than you are accounting for hence throwing the error (are you sure you know what parameters you're supposed to get [among all cases -- ex. when you get a "failed" response from your Web Service]).

And in addition to helping troubleshooting this problem, you will need to write Test Code for the Apex Class you just created from your WSDL. To do this, you'll need to create Mock Responses Test Classes where you simulate a response from your Web Service.

For example, I use a Web Service where I have one response for a login (where the WS gives me a SessionID), and then I have another response where I get an Activity File from my WS. And then I also have third response when I try to get an Activity File with an invalid SessionID. So, for this one Web Service, I need 3 Mock Responses Test Classes.

Related Topic