6

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.

  • Using Apex's DOM classes helps a bit when doing this manually. But I presume you would like to automate more starting from the WSDL? – Keith C Dec 18 '14 at 16:35
  • I would like to get examples of bodies as plain text as easy and convenient as possible. Then I would like to play around manually with the parameters in the text and see what happens in the text response. On how to obtain the request-bodies I'm totally open. AJAX Toolkit is one option. But are there others, maybe better? Just as an example, I want to figure out a body for create() (partner.wsld) to create a new SObject into the database. – Uwe Heim Dec 18 '14 at 16:39
  • DOM classes would give me the foundation to create something automated in APEX. But that is not necessary and I don't want to write my own Request-Generator. I would like to use something already existing, which builds me a string which I can paste into my APEX (or JavaScript or PHP) code. – Uwe Heim Dec 18 '14 at 16:43
7

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

| improve this answer | |
3

One idea / answer could be to use the JavaScript AJAX Toolkit and sniff the network traffic with Firebug. It brings up the request and response as XML text.

Here is an example to get the body for account creation:

createAccount : function() {
  var account = new sforce.SObject("Account");
  account.Name = "my new account";
  return sforce.connection.create([account]);       
},

This result in a body which can be captured in Firebug:

<se:Envelope xmlns:se="http://schemas.xmlsoap.org/soap/envelope/">
    <se:Header xmlns:sfns="urn:partner.soap.sforce.com">
        <sfns:SessionHeader>
            <sessionId>00D24000000IK6m!AQgAQCTkrmDFMy6GxcHjU9q5l2tYjITz79_CbfCVUMcjTwWRtCMRyeFauAIMFKI7cSUNO8Q6Me8MFvGwKGE9f1H2mOf5YKfz</sessionId>
        </sfns:SessionHeader>
    </se:Header>
    <se:Body>
        <create xmlns="urn:partner.soap.sforce.com" xmlns:ns1="sobject.partner.soap.sforce.com">
            <ns1:sObjects>
                <type>Account</type>
                <Name>my new account</Name>
            </ns1:sObjects>
        </create>
    </se:Body>
</se:Envelope>

I would like to know if someone knows different general techniques or tools to handle and tame those WSDLs in all their flavors.

| improve this answer | |
  • 3
    Did you try SoapUI yet? SOAPUI has the ability to import the WSDL file and generate the methods out of it automatically. You can then copy the generated xml for each method separately. – Aayush K Dec 18 '14 at 17:02
  • @AAU I'm looking exactly for this kind of recommendations, thanks. I will test SoapUI right now. Do I need the Pro version or is the free version good enough for my simple use case? – Uwe Heim Dec 18 '14 at 17:11
  • Free version is more than enough for this. In fact, using the free version you can also execute the methods and get the response. Although that will require some setup, like modifying the salesforce endpoint url in your soapUI and and adding the session id to each of your methods before you execute them. But to generate the methods, all you need is create a new project and import the WSDL. – Aayush K Dec 18 '14 at 17:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.