Dynamic SOAP message
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-25-2025 11:11 PM
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}.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-29-2025 05:03 AM - edited 09-29-2025 05:18 AM
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().
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.
Muhammad Iftikhar
If my response helped, please mark it as the accepted solution so others can benefit as well.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-29-2025 05:45 AM
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
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
and here
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-29-2025 06:00 AM
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
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-29-2025 08:27 PM
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.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
