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

Hi, indexOf() is standard javascript method and is supported in SNC server scripts,
and if you browse through OOB script includes you will find it is used frequently without arrayUtils().

/sys_script_include_list.do?sysparm_query=scriptLIKEindexOf&sysparm_view=

Your issue may be with how current.variables.incVar is assigned to array inc[]
and I would start by adding some debugging to validate your data

gs,.info(typeof current.variables.incVar);
gs.info(typeof inc);

and also confirm current.variables.incVar mapping to array 'inc',
and that inc hasn’t been created as an array of individual 'string characters.
// eg ['I','N','C','0','0','0','0','1']

gs.info(inc[0]); //etc etc

Assuming current.variables.incVar is the incident number? then perhaps try something like

var inc = current.variables.incVar; // or current.variables.incVar.toString();

gs.info(inc);
gs.info(typeof inc);


gs.info('INC ' + inc.indexOf('INC');
gs.info('notFound ' + inc.indexOf('notFound');

 

 

Couple questions:

1. where is the "current" object defined?  Can you include more of your Script Include to get a better idea as to what it is you have setup and how you are calling it?  I'm guessing "current" is undefined and you are getting Business Rules and Script Includes mixed up.

2. what type of Catalog Variable is "incVar"?  A string or reference variable?  If it is a reference, then you would end up with the sys_id instead of the number of the record, so you would never match with "indexOf"

We definitely need some more info and context as to what you are trying to do and with what information in order to help you more.

 current.variables.incVar it shoud be String 

 current.variables.incVar.toString();  then it will check if condition other wise every time its fail.

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

Pratik Malviya
Tera Guru

Hi @Rachael12 

 

You can follow below example.

 

var arr = ['a', 'b', 'c', '123', 'xyz'];
var arrUtil = new ArrayUtil();
gs.print(arrUtil.indexOf(arr, 'c')); 
Please mark the appropriate response as correct answer and helpful, This may help other community users to follow correct solution.
Thanks,
Pratik Malviya