- 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-22-2013 08:03 AM
Almost. That'll tell you which character position the letter "o" in "open" falls in the entire String. So for the Array:
{ "new", "active", "open", "closed" }
Which gives a toString() of:
new,active,open,closed
You'll get the answer 11 instead of 2, which is the position in the array that was wanted.
You'd also run into trouble if one of the array's values was "opentaco", since that'd return a positive match for the indexOf() check.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-13-2015 08:52 AM
Try this... I had a similar problem with using arrays and index of... this which my colleague wrote may help:
function contains(a, obj) {
var i = a.length;
while (i--) {
if (a[i] === obj) {
return true;
}
}
return false;
}
//then do
var array = [];
array.push("Badgers");
gs.log(contains(array,"Badgers"))

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-27-2015 02:46 PM
Michael:
Saw this and thought I would throw in my 2-cents on this as well. This is an ECMAScript 5 function. ServiceNow JavaScript is 3 or less (2.2.2). You can add this functionality with ArrayUtil, or extend the JavaScript language base with a prototype function. The MDN polyfill gives you most everything you need. I wrote about the prototype extension recently here.
Most of the methods missing (sic. ECMAScript 3 thru 6); if they have a Polyfill definition, can be added into the ServiceNow JavaScript base functionality.
Steven.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-02-2015 01:53 PM
Gotta love how the recent update to the developer portal broke so many links.
re-posting a working link for this interesting article by Steve Bell:
Mini-Lab: Extending the GlideRecord Object
Personally, I would like to see a ServiceNow Rhino compatible port of underscore.js.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-02-2015 04:36 PM
Wow! Thanks Kevin. Had not realized that the portal switchover had busted the links.