- 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
10-05-2016 09:20 AM
Scott:
Kevin stated in a response here earlier that if you find the functionality missing then dig out a polyfill to give you what is missing. He even listed the polyfill.
So that being said, Helsinki gives us most everything in ES5. I have not yet found any gaps. "use strict", const, etc. seem to be present. It is my understanding that the new Studio in Helsinki is actually written on top of ES6 (I have not dug into it to see if it is true).
I use a lot of Mozilla polyfills:
Google - Mozilla Polyfills
Some things to read up on:
Mini-Lab: Extending ServiceNow JavaScript Using ECMAScript 6 Polyfills
https://scotch.io/tutorials/javascript-transpilers-what-they-are-why-we-need-them
Steven.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-22-2016 01:33 PM
As others have hinted to, the Array.prototype is overwritten by Rhino, and they're on a very old version. Here's an article I wrote about the problem, and some solutions I provided: ServiceNow Pro Tips — Array.indexOf() not working in ServiceNow - Solution!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-23-2016 07:30 AM
Why not just polyfill your code? Paste this at the top of your server-side code block that needs Array.indexOf:
Array.prototype.indexOf() - JavaScript | MDN
Minified version
Array.prototype.indexOf||(Array.prototype.indexOf=function(r,t){var n;
if(null==this)throw new TypeError('"this" is null or not defined');
var e=Object(this),i=e.length>>>0;if(0===i)return-1;var a=+t||0;if(Math.abs(a)===1/0&&(a=0),a>=i)return-1;
for(n=Math.max(a>=0?a:i-Math.abs(a),0);i>n;){if(n in e&&e[n]===r)return n;n++}return-1});
Full version from MDN
// Production steps of ECMA-262, Edition 5, 15.4.4.14
// Reference: http://es5.github.io/#x15.4.4.14
if (!Array.prototype.indexOf) {
Array.prototype.indexOf = function(searchElement, fromIndex) {
var k;
// 1. Let o be the result of calling ToObject passing
// the this value as the argument.
if (this == null) {
throw new TypeError('"this" is null or not defined');
}
var o = Object(this);
// 2. Let lenValue be the result of calling the Get
// internal method of o with the argument "length".
// 3. Let len be ToUint32(lenValue).
var len = o.length >>> 0;
// 4. If len is 0, return -1.
if (len === 0) {
return -1;
}
// 5. If argument fromIndex was passed let n be
// ToInteger(fromIndex); else let n be 0.
var n = +fromIndex || 0;
if (Math.abs(n) === Infinity) {
n = 0;
}
// 6. If n >= len, return -1.
if (n >= len) {
return -1;
}
// 7. If n >= 0, then Let k be n.
// 8. Else, n<0, Let k be len - abs(n).
// If k is less than 0, then let k be 0.
k = Math.max(n >= 0 ? n : len - Math.abs(n), 0);
// 9. Repeat, while k < len
while (k < len) {
// a. Let Pk be ToString(k).
// This is implicit for LHS operands of the in operator
// b. Let kPresent be the result of calling the
// HasProperty internal method of o with argument Pk.
// This step can be combined with c
// c. If kPresent is true, then
// i. Let elementK be the result of calling the Get
// internal method of o with the argument ToString(k).
// ii. Let same be the result of applying the
// Strict Equality Comparison Algorithm to
// searchElement and elementK.
// iii. If same is true, return k.
if (k in o && o[k] === searchElement) {
return k;
}
k++;
}
return -1;
};
}