- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-18-2022 02:14 AM
Hi All!
I am trying to get the incident description field which contains
{"Impact":"SERVICES",<a target=\"_blank\" href=\"http://www.google.com\">Open in Browser</a></p>","ProblemID":"P-1234"}
When i do the below background script,
var string = '{"Impact":"SERVICES",<a target=\"_blank\" href=\"http://www.google.com\">Open in Browser</a></p>","ProblemID":"P-1234"}';
if (string.indexOf('{"Impact":"SERVICES"') > -1) {
var body = string.split('href=\"'); //find to spilt
body = body[1].split('\">Open'); //end split
body = body[0].toString(); // This is the string
gs.print(body);
}
And the result is good
However, if i put this in a Business before rule:
(function executeRule(current, previous /*null when async*/ ) {
//incident description is {"Impact":"SERVICES",<a target=\"_blank\" href=\"http://www.google.com\">Open in Browser</a></p>","ProblemID":"P-1234"}
var string = current.description;
if (string.indexOf('{"Impact":"SERVICES"') > -1) {
var body = string.split('href=\"'); //find to spilt
body = body[1].split('\">Open'); //end split
body = body[0].toString(); // This is the string
gs.log(body, 'test');
})(current, previous);
However the logs shows that the string is not spited. I have tried to do var string = current.description.toString(); but doesnt help as well.
Anyone know where i have done wrong?
Thanks!
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-18-2022 03:10 AM
Hi,
This issue is due to using the backslash character (\) while doing the split.
Try below code, its working for me.
Background Script -
var str = '{"Impact":"SERVICES",<a target=\"_blank\" href=\"http://www.google.com\">Open in Browser</a></p>","ProblemID":"P-1234"}';
var t = str.split("=");
var p = t[2].split('"');
gs.info(p[1]);
//Output
//*** Script: http://www.google.com
Business Rule -
(function executeRule(current, previous /*null when async*/ ) {
var desc = current.short_description;
if (desc.indexOf('{"Impact":"SERVICES"') > -1) {
var t = desc.split("=");
var p = t[2].split('"');
gs.log(p[1],'TEST123');
gs.log('Body = '+body,'TEST123');
}
})(current, previous);
Regards,
Sourabh
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-18-2022 02:20 AM
Hi Max,
Can you try to see what are you getting in body and string variables at different different places. This should help you understand where the code is going wrong.
Please mark this as correct and helpful if it resolved the query or lead you in right direction.
Thanks,
Mohit Kaushik
Community Rising Star 2022
Mohit Kaushik
ServiceNow MVP (2023-2025)

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-18-2022 02:25 AM
Hi,
I would suggest to avoid using reserved keywords like string, gr, gs etc in server side scripts.
Also put try catch to know/handle exceptions.
(function executeRule(current, previous /*null when async*/ ) {
//incident description is {"Impact":"SERVICES",<a target=\"_blank\" href=\"http://www.google.com\">Open in Browser</a></p>","ProblemID":"P-1234"}
try{
var desc= current.description.toString();
gs.info('Descriptin is : '+desc);
if (desc.indexOf('{"Impact":"SERVICES"') > -1) {
var body = desc.split('href=\"'); //find to spilt
body = body[1].split('\">Open'); //end split
body = body[0].toString(); // This is the string
gs.log(body, 'test');
}catch(e){
gs.error('Exception in BR : '+e);
}
})(current, previous);
Also put more logs and see what values you are getting.
Thanks,
Anil Lande
Thanks
Anil Lande
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-18-2022 02:54 AM
Thanks!
i did a try,
Descriptin is : {"Impact":"SERVICES",<a target=\"_blank\" href=\"http://www.google.com\">Open in Browser</a></p>","ProblemID":"P-1234"}
Error: Exception in BR : TypeError: Cannot read property "0" from undefined
and if i insert gs.info for body, this script after "var body = desc.split('href=\"'); //find to spilt"
i get the full string.. {"Impact":"SERVICES",<a target=\"_blank\" href=\"http://www.google.com\">Open in Browser</a></p>","ProblemID":"P-1234"}
so it seems like the split for ('href=\"') is not happening. is it because of the \" ?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-18-2022 02:59 AM
Hi,
did you try to add logs and check what came in description?
if the script worked in background script then it should work in BR as well.
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader