Customize response

Follow this example in order to customize and have control over the XML payload of the SOAP response.

  1. Create a customized XML document using the XMLDocument script include object.
  2. Set its document element to the variable response.soapResponseElement in a scripted web service.
    For example, the following scripted web service script:
    var xmldoc = new XMLDocument("<myResponse></myResponse>");
    xmldoc.createElement("element_one", "test"); // creates the new element at the document element level if setCurrent is never called
    xmldoc.createElement("element_two", "new2 value"); // calling without a value will create a new element by itself
    
    var el = xmldoc.createElement("element_three");
    xmldoc.setCurrent(el); // this is now the parent of any new elements created subsequently using createElement()
    xmldoc.createElement("newChild", "test child element");
    
    response.soapResponseElement = xmldoc.getDocumentElement();
    Is used to accept the following request:
    <soapenv:Envelope 
       xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" 
       xmlns:tes="http://www.service-now.com/TestCustomResponse">
       <soapenv:Header/>
       <soapenv:Body>
          <tes:execute/>
       </soapenv:Body>
    </soapenv:Envelope>
    Which will respond with the following SOAP response:
    <soapenv:Envelope 
        xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" 
        xmlns:tes="http://www.service-now.com/TestCustomResponse">
       <soapenv:Header/>
       <soapenv:Body>
          <myResponse>
             <element_one>test</element_one>
             <element_two>new2 value</element_two>
             <element_three>
                <newChild>test child element</newChild>
             </element_three>
          </myResponse>
       </soapenv:Body>
    </soapenv:Envelope>

    WSDL support will need to be created externally. The SOAP endpoint will need to be referred back to the scripted web service in question.