IndexOf() does not work in script include
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-18-2021 11:07 AM
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.
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-18-2021 03:15 PM
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');
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-18-2021 05:03 PM
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.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-24-2021 10:34 PM
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!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-21-2024 11:53 PM
Hi @Rachael12
You can follow below example.
var arr = ['a', 'b', 'c', '123', 'xyz'];
var arrUtil = new ArrayUtil();
gs.print(arrUtil.indexOf(arr, 'c'));
Thanks,
Pratik Malviya