- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-17-2013 02:47 PM
I realize this could be a general JavaScript question, but it seems like it should be working in the "real world" (and is failing within Service-Now). Basically, I'm using a business rule to look through an array of values from a custom status field I've added to [grc_remediation]:
var grRemediations = new GlideRecord('grc_remediation'); ... var aRemediationStatuses = new Array(); while (grRemediations.next()) { aRemediationStatuses.push(grRemediations.u_status.toLowerCase()); } ... gs.log('aRemediationStatuses: ' + aRemediationStatuses); // The statement above will return 'aRemediationStatuses: work in progress,open'. gs.log('aRemediationStatuses.indexOf("open"): ' + aRemediationStatuses.indexOf('open')); /* The statement above will return 'aRemediationStatuses.indexOf("open"): undefined'. Shouldn't it return 'aRemediationStatuses.indexOf("open"): 1'? */
I need to know where a value is showing up within an array (or if it's not in the array at all), but I just keep getting 'undefined' returned by .indexOf(), and I have verified that the array is properly populated. Any suggestions?
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-05-2016 08:52 AM
For anyone that runs across this post as I did today. A colleague of mine just mentioned .indexOf() works in Helsinki for arrays. I tried it between Geneva and Helsinki and indeed it does work!
var s='Detroit,London,Tokyo';
var s2 = s.split(',');
var a = s2.indexOf("London");
gs.print(a);
In Geneva I get "undefined"
In Helsinki I get "1"
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-17-2013 02:56 PM
Checkout ArrayUtil helper.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-17-2013 03:04 PM
Could be that whatever version of Rhino/JavaScript is currently being used in ServiceNow to evaluate JavaScript on the server doesn't support indexOf for Arrays.
If that's the case, you can use the ArrayUtil Script Include which has an indexOf function in it. Use would be like this:
gs.log('aRemediationStatuses.indexOf("open"): ' + ArrayUtil.indexOf(aRemediationStatuses, "open");
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-18-2013 01:01 PM
Thanks, guys! Sure enough, using the ArrayUtil() script include worked:
...
var oArrayUtil = new ArrayUtil();
gs.log('ArrayUtil.indexOf(aRemediationStatuses, "open"): ' + oArrayUtil.indexOf(aRemediationStatuses, 'open'));
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-21-2013 06:27 PM
This should work also:
aRemediationStatuses.toString().indexOf('open')