Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

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

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

Chetan Mahajan
Kilo Sage

Hi Max,

                     Try below code it's working for  me, created after insert/update BR on Incident

(function executeRule(current, previous /*null when async*/ ) {

    // var string = '{"Impact":"SERVICES",<a target=\"_blank\" href=\"http://www.google.com\">Open in Browser</a></p>","ProblemID":"P-1234"}';

    var string = current.getValue('description');

    if (string.indexOf('{"Impact":"SERVICES"') > -1) {

        var matches = string.match(/\bhttps?:\/\/\S+/gi); // prints http://www.google.com\">Open
        var body = matches.toString();
        body = body.replace('">Open', ''); // prints http://www.google.com\
    }
    gs.info("Output Value is : " + body);

})(current, previous);

Output : 

find_real_file.png

Kindly mark correct and helpful if applicable