
- Subscribe to RSS Feed
- Mark as New
- Mark as Read
- Bookmark
- Subscribe
- Printer Friendly Page
- Report Inappropriate Content
NOTE: MY POSTINGS REFLECT MY OWN VIEWS AND DO NOT NECESSARILY REPRESENT THE VIEWS OF MY EMPLOYER, ACCENTURE.
DIFFICULTY LEVEL: ADVANCED
Assumes having taken the class SSNF and has good intermediate to advanced level of knowledge and/or familiarity with Scripting in ServiceNow.
PRE-REQUISITE: I developed this example using Scripts - Background and requires the installation of the GlideRecordExtensions Script Include.
So here is a slick method for searching an object array for a specific value.
This is the Array.filter method. This is a Lambda like function that allows a developer to create a search function, pass that and a value to match against. filter will then utilize the passed search function to find the value in the given array of objects, and return all of the objects that were located. This is then passed back as an array of objects; a sub-set of the original.
The following code represents a way of using the Array.filter method (I also thew-in the use of my GlideRecord extension .toObjectList to help facilitate converting the Incident record-set into an object array):
// bring in our GlideRecord extension functions
gs.include('GlideRecordExtensions');
// get all of the active incident records
var incidents = new GlideRecord('incident');
incidents.addActiveQuery();
incidents.query();
gs.info('---> Total Active Incidents Loaded into list: ' + incidents.getRowCount());
// this will be the value that we will search for
var match = 'INC0000017';
var incidentList = incidents.toObjectList(); // convert to object array
var filteredList = incidentList.filter(findIt, match); // now find it!
gs.info('---> Total found: ' + filteredList.length);
// print off the number of everything we found (we should only have one match)
for (var i = 0; i < filteredList.length; i++) {
gs.info('---> [i:{0}] Found Incident Number (from record): {1}', [i, filteredList[i].number]);
}
// this particular function looks at the incident.number field
// the "this" variable is our "match" parameter
function findIt(value) {
if (value.number == this) {
return value;
}
}
Result:
*** Script: ---> Total Active Incidents Loaded into list: 252
*** Script: ---> Total found: 1
*** Script: ---> [i:0] Found Incident Number (from record): INC0000017
NOTE: To pass in more than one parameter for the search you create an object and pass that in. The object would contain multiple properties that would represent other match criteria.
NOTE: ServiceNow uses the JavaScript standard JavaScript 1.5 - ECMAScript 3.1.5 (or some of it), also ECMAScript 2.6.2, ECMA 5 and ECMAScript 2021 (only with Scoped applications and you have to opt-in). If you want to see more of what ServiceNow contains go to any ServiceNow instance and navigate to System Diagnostics > Stats > Stats and click on the Open Source software link. This will display a rather lengthy PDF of all of the licenses that ServiceNow employes in the platform.
If you are interested; the next version features of JavaScript you can find it here ECMAScript 6; where we actually see a Lambda function! It makes this process even better by refining how the function is passed. Again, this functionality is available, but only for Scoped applications (as of San Diego, I believe).
You can find out more about what the "filter" method provides here:
If you want to know more about the JavaScript language versions see: Wikipedia
Enjoy!
Steven Bell.
If you find this article helps you, don't forget to log in and mark it as "Helpful"!
Originally published on: 07-22-2015 07:02 AM
I updated the code and brought the article into alignment with my new formatting standard.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.