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 11:18 AM
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.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-18-2021 12:06 PM
Hai
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!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-18-2021 12:12 PM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-18-2021 12:20 PM
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.