sabell2012
Mega Sage
Mega Sage

 

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.


Awhile ago I wrote on how to extend the JavaScript language base inside of ServiceNow with several functions available via ECMA 6 and Mozilla Polyfill definitions. Well, here is another I found myself needing recently: The Array.findIndex function.

 

What does this bring to the table? Well, it allows you to basically do an indexOf on a complex object array. This allows you to do two things: (1) bring back the location of the record by giving the findIndex the object property and value, and/or (2) determine if a record exists in an object array given an object property and value.

 

For example:

 

var ID = <<sys_id>>
var array = <<list of objects with a property of ID>>
var index = array.findIndex(function(element) { return element.ID == ID; });

 

This would return the array index of the first match.

 

 

Lab 1.1: Add the Polyfill Script to the JavaScriptExtensions Library

 

1. If you have not already done a pre-requisite to work your way through my Polyfill Mini-Lab. Go ahead and work through that mini-lab article:

 

Mini-Lab: Extending ServiceNow JavaScript Using ECMAScript 6 Polyfills

The polyfill we will be using can be found here:

https://reference.codeproject.com/javascript/Reference/Global_Objects/Array/findIndex 

 

2. Update the JavaScriptExtensions Script Include to contain the new prototype.

 

a. Obtain the polyfill code from the following location: link

b. Add the code to the JavaScriptExtensions script.

 

Here is the script from the polyfill article:

 

if (!Array.prototype.findIndex) {
	Array.prototype.findIndex = function(predicate) {
		'use strict';
		if (this == null) {
				throw new TypeError('Array.prototype.findIndex called on null or undefined');
		}
		if (typeof predicate !== 'function') {
				throw new TypeError('predicate must be a function');
		}
		var list = Object(this);
		var length = list.length >>> 0;
		var thisArg = arguments[1];
		var value;

		for (var i = 0; i < length; i++) {
				value = list[i];
				if (predicate.call(thisArg, value, i, list)) {
						return i;
				}
		}
		return -1;
	};
}

 

c. Navigate to System Definition -> Script Includes.

d. Edit up the Script Include JavaScriptExtensions.

e. Paste the new findIndex polyfill definition on the end of the script. Your Script Include should look like this:

 

sabell2012_1-1699548957170.png

f. Click the Update button to save your work.

 

Lab 1.2: Testing the findIndex Functionality

 

1. Navigate to Scripts - Background and copy and paste the following code into the script text box on the form.

 

Script:

 

gs.include('JavaScriptExtensions');

var incidentList = getIncidentList(10);
var incidentID = '0c43b55ac6112275019abd2b247baf31';

var index = incidentList.findIndex(function(element) { return element.sys_id == incidentID; });
gs.info('---> index: ' + index);

// get a list of [limit] number of cmdb ci records
function getIncidentList(limit) {
	var incidentRecords = new GlideRecord('incident');
	incidentRecords.addNotNullQuery('cmdb_ci');
	incidentRecords.addActiveQuery();
	incidentRecords.setLimit(limit);
	incidentRecords.query();

	var cmdbList = [];

	// The following looping code is a GlideRecord inside of a GlideRecord.
	// This is normally a BAD way of doing this, but for the purposes of this test...
	while (incidentRecords.next()) {
		var ciRecords = new GlideRecord('cmdb_ci');
		if (ciRecords.get('sys_id', incidentRecords.getValue('cmdb_ci'))) {
			cmdb_ci = {};
			cmdb_ci.sys_id = ciRecords.getValue('sys_id');
			cmdb_ci.serial_number = ciRecords.getValue('serial_number');
			cmdb_ci.name = ciRecords.getValue('name');
			cmdb_ci.className = ciRecords.getValue('sys_class_name');
			cmdb_ci.install_status = ciRecords.getValue('install_status');
			cmdbList.push(cmdb_ci);
		}
	}

	gs.info('---> cmdbList: ' + JSON.stringify(cmdbList));

	return cmdbList;
}

 

Make sure that you pick Global for the scope.

Click the Run script button at the bottom of the form.

 

You will get the following results (I chose an Incident that comes OOB - if it doesn't work go get one that does):

 

*** Script: ---> cmdbList: [{"sys_id":"46c7318aa9fe198100c76003f0bc82e9","serial_number":null,"name":"Saints and Sinners Bingo","className":"cmdb_ci_spkg","install_status":"1"},{"sys_id":"469facd7a9fe1981015830c43428ca2d","serial_number":"L3CD257","name":"IBM-T42-DLG","className":"cmdb_ci_computer","install_status":null},{"sys_id":"46b8e13ba9fe1981005990e20d315e4a","serial_number":null,"name":"WeatherBug","className":"cmdb_ci_spkg","install_status":"1"},{"sys_id":"0c43b55ac6112275019abd2b247baf31","serial_number":null,"name":"EFOWEB","className":"cmdb_ci_computer","install_status":"1"},{"sys_id":"b0c4030ac0a800090152e7a4564ca36c","serial_number":null,"name":"MailServerUS","className":"cmdb_ci_server","install_status":"1"},{"sys_id":"0c43b896c6112275019abd2b2b93f464","serial_number":null,"name":"WEBSERVER","className":"cmdb_ci_computer","install_status":"1"},{"sys_id":"b0c25d1bc0a800090168be1bfcdcd759","serial_number":null,"name":"FileServerFloor2","className":"cmdb_ci_server","install_status":"1"},{"sys_id":"a9a2d111c611227601fb37542399caa8","serial_number":"L3BB911","name":"*JEMPLOYEE-IBM","className":"cmdb_ci_computer","install_status":"1"},{"sys_id":"a9c0c8d2c6112276018f7705562f9cb0","serial_number":null,"name":"Sales Force Automation","className":"cmdb_ci_service","install_status":"1"},{"sys_id":"a9c0c8d2c6112276018f7705562f9cb0","serial_number":null,"name":"Sales Force Automation","className":"cmdb_ci_service","install_status":"1"}]
*** Script: ---> index: 3

 

The script first gets a sys_id to look for, then plugs that into polyfill findIndex function.

 

Pretty cool, huh?

 

Enjoy!

Steven Bell.

 

If you find this article helps you, don't forget to log in and mark it as "Helpful"!

 

sabell2012_0-1699547737960.png


Originally published on: 8-30-2016 06:41 AM

I updated the code and brought the article into alignment with my new formatting standard.