[SalesForce] How to form SOAP XML request to update Salesforce dara

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
        w.writeCharacters(a.Id);
        //close </accountid>
        w.writeEndElement();
        //close </account>
        w.writeEndElement();
    }
    //close </accounts>
    w.writeEndElement();
    w.writeEndDocument();
    String xmlOutput = w.getXmlString();

    w.close();
    //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.
    //request.setEndpoint('https://imshealth--dev25.cs30.my.salesforce.com/services/data/v20.0/query');
    request.setEndpoint('https://imshealth--dev25.cs30.my.salesforce.com/services/Soap/u/34.0/');
    // This line is important to tell the server you are passing XML input.
    request.setHeader('Content-Type', 'text/xml');
    request.setMethod('POST');
    request.setHeader('X-SFDC-Session', UserInfo.getSessionId());
    request.setHeader('SoapAction', 'test');
    // Set the XMLOutput we have created in the class above

    request.setBody(xmlOutput);
    //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());
    readResponse(reader);
}

Best Answer

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"

<SOAP-ENV:Envelope
  xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
  SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
   <SOAP-ENV:Body>
       <m:GetLastTradePrice xmlns:m="Some-URI">
           <symbol>DIS</symbol>
       </m:GetLastTradePrice>
   </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

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

Related Topic