[SalesForce] How to create HttpRequest body as plain string from WSDL

Is there an good way to (automatically) create proper request bodies from WSDL files just as a plain xml string? I need this for perfoming calls from APEX to different salesforce APIs, mostly for the Partner SOAP API. Here is an example which I figured out (just to demonstrate what I mean):

public static String login(String username, String passwordAndToken) {
    Http h = new Http();
    HttpRequest req = new HttpRequest();
    URL baseUrl = URL.getSalesforceBaseUrl();
    req.setEndpoint( baseUrl.toExternalForm() + '/services/Soap/u/'+'30.0');
    req.setMethod('POST');
    req.setHeader('Content-Type', 'text/xml'); 
    req.setHeader('SOAPAction', '""');
    string body = ''
        +'<se:Envelope xmlns:se="http://schemas.xmlsoap.org/soap/envelope/">'
          +'<se:Header xmlns:sfns="urn:partner.soap.sforce.com">'
            +'<sfns:SessionHeader>'
              +'<sessionId>'+'</sessionId>'
            +'</sfns:SessionHeader>'
          +'</se:Header>'
          +'<se:Body>'
            +'<login xmlns="urn:partner.soap.sforce.com" xmlns:ns1="sobject.partner.soap.sforce.com">'
              +'<username>'+username+'</username>'
              +'<password>'+passwordAndToken+'</password>'
            +'</login>'
           +'</se:Body>'
        +'</se:Envelope>'
    ;   
    req.setBody(body);
    HttpResponse res = h.send(req);
    return res.getBody();
}   

My question is all about the body. So as you see, I'm generating the body string for the API call for the function login defined in the partner.wsdl.

Now the partner.wsdl is full of other functions which I want to call. Composing the body strings manually is kind of time wasting and error prone.

Is there a good way (or tool) to generate and capture those strings?

I know there are also build in mechanisms to create APEX classes with Wsdl2Apex but for many reasons I would prefer it the text-way because there are a lot of complex types in the salesforce APIs and Wsdl2Apex is simply not doing the best job. I just don't want to do it all manually by hand.

Best Answer

The FuseIT Wsdl2Apex tool can generate HttpRequests using the DOM classes. To do this you need to select the HTTP Request option for the required methods.

enter image description here

Here is an example of how the generated HTTPRequest version of the login method appears. enter image description here

Each request and response element gets a constructor that works with a Dom.XmlNode and a corresponding method to populate a Dom.XmlNode. I found you could use straight string processing for reasonably simple requests and responses. With more complex parameters and responses it was easier to have an object that could serialize and deserialize the XML.


That said, it can generate WebServiceCallout.invoke compatible classes for the entire Partner API. Is there a particular web method that is causing you issues?

Once you have the WSDL2Apex generated classes working you can capture the CALLOUT_REQUEST and CALLOUT_RESPONSE log messages if you want the strings.


If you are feeling inspired there is also the open source version of WSDL2Apex. You could generate the string version of the requests directly from there.


I'll second @AAU's comment about SoapUI. You add the WSDL in and it will generate the required Request stub for you to fill in the parameters.

Salesforce SOAP API Request using SoapUI