Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Dynamic SOAP message

Nitesh Kumar4
Tera Contributor

Hello Friends,

Need your help, below code is not working dynamic, any mistake in request body. Creating the record in external system but caller and short_description is not dynamic it is inserting ${sd}, ${caller_id}.

 

var sp = new sn_ws.SOAPMessageV2();
    var user = "inc.integration";
    var pass = "xxxxxxxxxxxxxx";
    sp.setBasicAuth(user, pass);
    sp.setStringParameterNoEscape('sd', current.short_description);
    sp.setStringParameterNoEscape('cid', current.caller_id);

    sp.setRequestBody('<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:inc="http://www.service-now.com/incident"><soapenv:Header/><soapenv:Body><inc:insert><caller_id>${cid}</caller_id><short_description>${sd}</short_description></inc:insert></soapenv:Body></soapenv:Envelope>');

    var response = sp.execute();
    var responseBody = response.getBody();

    var xmlDoc = new XMLDocument2();
    xmlDoc.parseXML(responseBody);
    gs.addInfoMessage(xmlDoc.getNodeText('//sys_id'));
    gs.addInfoMessage(xmlDoc.getNodeText('//number'));
6 REPLIES 6

M Iftikhar
Tera Sage

Hi Nitesh,

The issue you're encountering stems from the way the setRequestBody() method works in the sn_ws.SOAPMessageV2 API. As per the ServiceNow documentation, when you use the setRequestBody() method, parameter substitution (e.g., ${sd}, ${cid}) does not occur automatically. The placeholders are treated as literal strings and are not replaced by the values set via setStringParameterNoEscape().

MIftikhar_0-1759147339901.png

 

To resolve this, you need to construct the SOAP request body by concatenating the dynamic values directly into the XML string. Here’s how you can update your code:

 

 

var requestBody = 
    '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:inc="http://www.service-now.com/incident">' +
        '<soapenv:Header/>' +
        '<soapenv:Body>' +
            '<inc:insert>' +
                '<caller_id>' + current.caller_id + '</caller_id>' +
                '<short_description>' + current.short_description + '</short_description>' +
            '</inc:insert>' +
        '</soapenv:Body>' +
    '</soapenv:Envelope>';

sp.setRequestBody(requestBody);

 

Thanks & Regards,
Muhammad Iftikhar
If my response helped, please mark it as the accepted solution so others can benefit as well.

Thanks & Regards,
Muhammad Iftikhar

If my response helped, please mark it as the accepted solution so others can benefit as well.

ChrisBurks
Giga Sage

If you're trying to use template literals and are writing your script within an editor that gives you the choice to use ES12 mode

ChrisBurks_0-1759149375939.png

 

You would need to substitute the single quotes ( ' )  quotes that is wrapping your xml with tick marks ( ` ). This mark is usually found on an US keyboard to the left of the number 1 key. 

The tick marks will allow you to substitute ${var_name} without having to concatenate with the plus ( + ) sign. 

 

...

  sp.setRequestBody(`<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:inc="http://www.service-now.com/incident"><soapenv:Header/><soapenv:Body><inc:insert><caller_id>${cid}</caller_id><short_description>${sd}</short_description></inc:insert></soapenv:Body></soapenv:Envelope>`);

...

 

Look closely at 

  sp.setRequestBody(`<soapenv:Envelope

and here

 </soapenv:Envelope>`);
 
Using the tick marks will allow any variable wrapped in ${} to be replaced with the value of the correlating variable. 
 
 

Ankur Bawiskar
Tera Patron
Tera Patron

@Nitesh Kumar4 

your script is wrong.

2 ways to achieve

1) Form the request body and don't use Variable Substitution

var sp = new sn_ws.SOAPMessageV2();
sp.setEndpoint('https://xxxxxx.service-now.com/incident.do?SOAP');
sp.setSOAPAction('http://www.service-now.com/incident/insert');
var user = "inc.integration";
var pass = "xxxxxxxxxxxxxx";
sp.setBasicAuth(user, pass);
// sp.setStringParameterNoEscape('sd', current.short_description); // not required
// sp.setStringParameterNoEscape('cid', current.caller_id); // not required

sp.setRequestBody('<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:inc="http://www.service-now.com/incident"><soapenv:Header/><soapenv:Body><inc:insert><caller_id>' + current.caller_id + '</caller_id><short_description>' + current.short_description + '</short_description></inc:insert></soapenv:Body></soapenv:Envelope>');

var response = sp.execute();
var responseBody = response.getBody();

var xmlDoc = new XMLDocument2();
xmlDoc.parseXML(responseBody);
gs.addInfoMessage(xmlDoc.getNodeText('//sys_id'));
gs.addInfoMessage(xmlDoc.getNodeText('//number'));

OR

2) Use variable substitution and then use setStringParameterNoEscape method to set the values dynamically, but remember you need to add the complete body in the SOAP Message and you will just replace the dynamic values.

Variable substitution in outbound SOAP 

Outbound SOAP message - variable substitution and scripting. 

Like this in Content

AnkurBawiskar_0-1759150803586.png

 

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

@Nitesh Kumar4 

Hope you are doing good.

Did my reply answer your question?

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader