Escaping special characters in XML
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-31-2021 11:36 PM
Hello, i need to escape special characters from string to safely insert them into XML document (for example "&" to "&" etc). Is there any build in function to do that?
- Labels:
-
Scripting and Coding

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-31-2021 11:42 PM
Hello,
You can try using this API (XMLUtilJS):
https://developer.servicenow.com/dev.do#!/reference/api/orlando/server_legacy/c_XMLUtilJSAPI
Regards!
Kristian
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-31-2021 11:48 PM
Hi,
Are you using this in the outbound rest or soap message?
if yes then you can use this function to escape the XML characters
setStringParameter()
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-31-2021 11:48 PM
Hi,
setStringParameter() -> to set simple string values such as sys_created_by which won't have special characters
XML reserved characters in the value are converted to the equivalent escaped characters.
setStringParameterNoEscape() -> to set work notes etc where end user can enter special character and in case you want to send that special character as it is without escaping
It does not escape XML reserved characters.
XML reserved characters are:
>
<
&
%
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-01-2021 12:08 AM
I'm using it in flow designer. Getting it in script step and than populating xml message in rest step.