- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-13-2017 01:08 PM
I've used a similar script in the past which works but I'm not sure why I'm not getting the results this time around. I'm placing variable results into an array and attempting to evaluated the array with includes(). The result is never yes even though the array contains the correct value. I'm running this as a background script for testing purposes.
var array = [];
var gr = new GlideRecord('sc_req_item');
gr.addQuery('request', 'd65ec5b7130dc3000b63b86f3244b076');
gr.query();
while (gr.next()) {
array.push(gr.variables.request_type_hardware);
}
gs.print(array);
if (array.includes('replacement_upgrade')) {
gs.print('yes');
}
}
This is the array result:
Solved! Go to Solution.
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-13-2017 01:26 PM
That is entirely possible, though I would assume it's system-wide, as SN Rhino engine only supports ECMAScript 2015. I could be way out to lunch here, though
I just did this test in a background script:
var arrayUtil = new ArrayUtil();
var aNew = ['apples', 'oranges', 'bananas'];
if (aNew.includes('apples')) {
gs.print("includes");
}
if (arrayUtil.contains(aNew, 'apples')) {
gs.print("Util");
}
Only 'Util' was printed to the output.
Cheers,
Tim
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-13-2017 01:50 PM
So this is the revised one using ArrayUtil: It's evaluating to 'no' regardless of the actual result:
var array = [];
var arrayutil = new ArrayUtil();
var gr = new GlideRecord('sc_req_item');
gr.addQuery('request', current.number);
gr.query();
while (gr.next()) {
array.push(gr.variables.request_type_hardware == 'replacement_upgrade');
}
if (arrayutil.contains(array, true)) {
return 'yes';
}
return 'no';
}
Runs fine in a background script
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-13-2017 01:58 PM
Debug logs seems to indicate that the array isn't defined...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-13-2017 02:20 PM
Just looking at the code you pasted above, you won't get a result from:
gr.addQuery('request', current.number)
You'd have to pass the sys_id of the request, or:
gr.addQuery('request.number', current.request.number)
I take it you're working from two places to debug this? Workflow and background scripts? Just making sure you're not getting your objects (current vs gr) mixed up somewhere.
Tim
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-13-2017 02:47 PM
Good catch. It's current.sys_id since it's already running on the request table.
Works now. Thanks!

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-13-2017 03:08 PM
Can you please try like this.
- var array = [];
- var gr = new GlideRecord('sc_req_item');
- gr.addQuery('request', current.request);
- gr.query();
- while (gr.next())
- array.push(gr.variables.request_type_hardware);
- gs.print(array);
- if(array.indexOf('replacement_upgrade') > -1)
- gs.print('yes');