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.

IndexOf() does not work in script include

Rachael12
Tera Contributor

Hello,

I am writing a code in script include and need to check if my value has 'INC' in it, so I tried using IndexOf with -1,0,1 but it did not work with any of these. Also I tried using split and startsWith as well but it did not work.

value is 'INC - 12XXXX'

Please suggest.

8 REPLIES 8

Brad Bowman
Kilo Patron
Kilo Patron

Hi Rachael,

It would help to see your entire script, but in the meantime make sure you're using the correct method name .indexOf (case-sensitive), and that the value is first a string, using .toString() to force it to such if necessary.

sekhar kurumoju
Mega Guru

Hai @Rachel 

The indexOf() method returns the position of the first occurrence of a specified value in a string.

This method returns -1 if the value to search for never occurs.

Please Share Your script we can help more

BR

Sekhar Kurumoju

Please mark reply as Helpful/Correct, if applicable. Thanks!

 

Rachael12
Tera Contributor

Hi,

I was using simple script

var inc = current.variables.incVar;
if(inc.indexOf('INC') > -1)
task.assignment_group = 'XXXXXXXXXXX';

But it does not work. Then I read somewhere that indexOf is not supported in server scripts and need to use ArrayUtil.

var arrUtil = new ArrayUtil();
var inc = [];
inc = current.variables.incVar;

var asd = ['INC'];

if (arrUtil.indexOf(inc, asd) >= 0) {
task.assignment_group = 'XXXXXXXXXXX''; 
}

But it keeps going in else condidtion. Please advice.

indexOf works fine in server scripts.  You may just need to force the value to a string before you attempt to execute a string method as I said earlier, but you should also find out if 'current' is referencing anything, or what you think it is, since this is a Script Include and not a workflow Run Script or Business rule.  Use this syntax on the assignment, followed by a log.

var inc = current.variables.incVar.toString();
gs.info('Script Include inc= ' + inc);

If the log doesn't show a value, or you're not even getting a log then you'll need to pass in the RITM, or do a GlideRecord to retreive it, depending on how you are using this SI.