Is anyone has some idea how can i use DOM or XmlStreamWriter to generate SOAP XML request for Salesforce?

Can we use these classes to generate SOAP XML request or not?

I tried following code but its not working -

public void sendRequest() {
    // Get the list of accounts for which we need the updated values from the external system
    List < Account > accountList = new List <Account>([Select Id from Account limit 1]);
    // Start preparing the XML
    XmlStreamWriter w = new XmlStreamWriter();
    w.writeStartDocument(null, '1.1'); //Start the XML document
    w.writeStartElement(null, 'accounts', null); //this will start with <accounts> in XML
    //Loop through the accounts we queried earlier and put the values into XML
    for (Account a: accountList) {
        // This will write <account> in the XML file
        w.writeStartElement(null, 'account', null);
        // Open the tag <accountID> that will be identified by the external system
        w.writeStartElement(null, 'accountid', null);
        // Write the value of externalID to be send
        //close </accountid>
        //close </account>
    //close </accounts>
    String xmlOutput = w.getXmlString();

    //Now that we have XML we will pass this to external system using HttpRequest
    System.HttpRequest request = new System.HttpRequest();
    // Set the endpoint URL previously decided that will know how to handle the XML
    // we send them. Basically it will contain the code to read the values we are
    // sending and send the data back.
    // This line is important to tell the server you are passing XML input.
    request.setHeader('Content-Type', 'text/xml');
    request.setHeader('X-SFDC-Session', UserInfo.getSessionId());
    request.setHeader('SoapAction', 'test');
    // Set the XMLOutput we have created in the class above

    //system.assertEquals(null, request.toString());
    //This is like dialing on a telephone, we send the data and wait for the response.
    System.HttpResponse response = new System.Http().send(request);
    //this.Response = response.getBody();
    //And here we read the response. We will then process the response
    XmlStreamReader reader = new XmlStreamReader(response.getBody() );
    system.assertEquals(null, response.getBody());

You can construct a SOAP request using those classes. Using the DOM classes is easier because using those you do not need to work at such a detailed level in your code.

Here is an example of a SOAP message from the Examples of SOAP Messages section of the "Simple Object Access Protocol (SOAP) 1.1" spec:

POST /StockQuote HTTP/1.1
Host: www.stockquoteserver.com
Content-Type: text/xml; charset="utf-8"
Content-Length: nnnn
SOAPAction: "Some-URI"

       <m:GetLastTradePrice xmlns:m="Some-URI">

Note the very specific namespaces that must be used, the Envelope and Body elements that must be present, and the SOAPAction header. You will need to get all the details right for your request to work, and also your response parser will have to deal with similar details.

(SOAP is usually handled by using code generators such as WSDL2Apex because of the complexity. Unfortunately WSDL2Apex does not handle many elements commonly present in WSDLs.)

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.