Welcome to Community Week 2025! Join us to learn, connect, and be recognized as we celebrate the spirit of Community and the power of AI. Get the details  

How to replace special characters that are blocked by XML in business rule ?

Naina3
Kilo Contributor

Hi,

I am sending fields value to another SNOW instance using SOAP message. I have used Business rule to insert and update the values of fields. I am sending values of few fields in work notes. It is not working for the values containing either & or white space. I tried to replace these as mentioned below but it is not working. Can anyone help me with this?

current.work_notes.toString().replace(/[&]/g, '&');
current.work_notes.toString().replace(/[ ]/g, ' ');

 

Thanks in advance!

 

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

Hi Naina,

I believe you are saying about these 5 special characters which needs to be escaped before sending in xml or else xml string will break

"   "
'   '
<   &lt;
>   &gt;
&   &amp;

var data = current.work_notes.toString();
if(data.indexOf("'") > 0){
data = data.replaceAll("'","&apos;");
}
else if(data.indexOf("\"") > 0){
data = data.replaceAll("\"","&quot;");
}
else if(data.indexOf("<") > 0){
data = data.replaceAll("<","&lt;");
}
else if(data.indexOf(">") > 0){
data = data.replaceAll(">","&gt;");
}
else if(data.indexOf("&") > 0){
data = data.replaceAll("&","&amp;");
}

Mark Correct if this solves your issue and also mark Helpful if you find my response worthy based on the impact.
Thanks
Ankur

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

View solution in original post

1 REPLY 1

Ankur Bawiskar
Tera Patron
Tera Patron

Hi Naina,

I believe you are saying about these 5 special characters which needs to be escaped before sending in xml or else xml string will break

"   &quot;
'   &apos;
<   &lt;
>   &gt;
&   &amp;

var data = current.work_notes.toString();
if(data.indexOf("'") > 0){
data = data.replaceAll("'","&apos;");
}
else if(data.indexOf("\"") > 0){
data = data.replaceAll("\"","&quot;");
}
else if(data.indexOf("<") > 0){
data = data.replaceAll("<","&lt;");
}
else if(data.indexOf(">") > 0){
data = data.replaceAll(">","&gt;");
}
else if(data.indexOf("&") > 0){
data = data.replaceAll("&","&amp;");
}

Mark Correct if this solves your issue and also mark Helpful if you find my response worthy based on the impact.
Thanks
Ankur

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