String spilt works in background script, but not working in Business rule

Max Lin
Tera Contributor

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
find_real_file.png

 

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! 

1 ACCEPTED SOLUTION

Sourabh26
Giga Guru

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

View solution in original post

6 REPLIES 6

Mohit Kaushik
Mega Sage
Mega Sage

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

Thanks,
Mohit Kaushik
ServiceNow MVP (2023-2025)

Anil Lande
Kilo Patron

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

Please appreciate the efforts of community contributors by marking appropriate response as correct answer and helpful, this may help other community users to follow correct solution in future.
Thanks
Anil Lande

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 \" ? 

Ankur Bawiskar
Tera Patron
Tera Patron

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

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