
- Post History
- Subscribe to RSS Feed
- Mark as New
- Mark as Read
- Bookmark
- Subscribe
- Printer Friendly Page
- Report Inappropriate Content
on 09-04-2020 08:18 AM
ArrayUtil
ArrayUtil is an Out of the Box [OOB] script include which provides the ability to perform various cool functions related to Arrays. These functions will save you tons of custom lines of scripts and algorithms to play with arrays. It is documented HERE but I think some functions are not clear and need more details and that is what the goal of this article.
ArrayUtil script include can be found at:
https://<your_instance_name>.service-now.com/nav_to.do?uri=sys_script_include.do?sys_id=fb32a2d8c0a80a6000e907036f484b5b
ArrayUtil class requires an object to be created to access/call its functions.
If you are writing a script in Global scope and need to leverage ArrayUtil then you can call it with its name.
var arr = new ArrayUtil();
If you need to call it from custom scope then you call it with its API NAME.
var arr = new global.ArrayUtil();
This Utility contains 9 Methods/Functions which I will describe with example code and respective output. This Article Majorly talks about
- ensureArray method
- convertArray method
- diff method
- unique method
TRY EXAMPLE SCRIPTS IN BACKGROUND SCRIPT FOR A QUICK TEST
1. ensureArray | want to convert a javascript object into an array?
This function takes an object as a single parameter. It converts the object and returns as a new Array. Please note it returns the whole object converted array at index 0.
Script
/*
StudentInfoObj --> Object Name
arr --> Object of ArrayUtil Class to access its function.
studentInfoArray --> Array converted from Object.
Please note - index starts at 0 in an Array.
*/
var studentInfoObj = { 'name': 'Sharjeel', 'id': "RS030" }
var arr = new ArrayUtil();
var studentInfoArray = arr.ensureArray(studentInfoObj /*Object Name*/);
gs.info("Name is " + studentInfoArray[0]['name']);
gs.info("ID is " + studentInfoArray[0]['id']);
Output
2. covertArray | want to convert a java object into an array?
Script
/*
StudentInfoObj --> Object Name
arr --> Object of ArrayUtil Class to access its function.
studentInfoArray --> Array converted from Object.
Please note - index starts at 0 in an Array.
*/
// getMyGroups return loggedin user as JAVAOBJECT.
var myGroups = gs.getUser().getMyGroups();
gs.print(myGroups);
//print the type of myGroups object.
gs.print(Object.prototype.toString.call(myGroups));
// convert the JAVAOBJECT into the Array
var convertedArr = new ArrayUtil().convertArray(myGroups);
gs.print(convertedArr);
//print the type of convertedArray.
gs.print(Object.prototype.toString.call(convertedArr));
Output
To understand the difference between ensureArray & convertedArray see the example script below.
Script
// Javascript object NOT JAVA OBJECT
var studentInfo = {
"Name" : "Muhammad",
"ID": "RS001"
};
var arrayUtil = new ArrayUtil();
var cArray = arrayUtil.convertArray(studentInfo); //Array returned by convertArray method
var eArray = arrayUtil.ensureArray(studentInfo); //Array returned by ensureArray method
gs.print('convertArray String --> ' + JSON.stringify(cArray)); // See the value of converted array in string format
gs.print('convertArray Type --> ' + Object.prototype.toString.call(cArray)); // See the type of converted array in string format
gs.print('ensureArray String --> ' + JSON.stringify(eArray)); // See the value of ensured array in string format
gs.print('ensureArray Type --> ' + Object.prototype.toString.call(eArray)); // See the type of ensured array in string format
Output
3. diff | Want to remove duplicates between two or more arrays?
This function takes two or more arrays as parameters and returns the array of elements found in the first array but not in the rest of the arrays.
Script
/*
studentNames1 --> 1st Array of Names.
studentNames2 --> 2nd Array of Student IDs.
arr --> Object of ArrayUtil Class to access its function.
duplicatesRemoved --> Arrays of the element found in the first array but not in second.
Please note - Array passed in the second parameter will be appended/concatenated to first array.
*/
var studentNames1 = ['Muhammad', 'Sharjeel', 'Michael', 'Keshav'];
var studentNames2 = ['Sharjeel', 'Alex', 'Mubeen', 'Keshav'];
var arr = new ArrayUtil();
var duplicatesRemoved = arr.diff(studentNames1 /*1st Array*/, studentNames2 /*2nd Array*/);
gs.info("Student Concatenated Array --> " + duplicatesRemoved);
Output
4. unique | Want to remove duplicate elements in an array [single array]?
This function takes an array as a single parameter and returns the unique elements of the array [removed duplicates].
Script
/*
studentNames --> Array of Names, contains duplicate values.
arr --> Object of ArrayUtil Class to access its function.
uniqueElements --> Array of unique element, contains no duplicate.
*/
var studentNames = ['Muhammad', 'Sharjeel', 'Michael', 'Keshav', 'Sharjeel', 'Michael'];
var arr = new ArrayUtil();
var uniqueElements = arr.unique(studentNames /* Array Name */);
gs.info("Unique Names are --> " + uniqueElements);
Output

REF - https://developer.servicenow.com/dev.do#!/reference/api/orlando/server_legacy/c_ArrayUtilAPI
Feel free to Bookmark, hit helpful, and leave feedback or question in the comment box below.
- 8,503 Views

- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
One thing to note is that with the instance at ECMAScript 5ish there are built in array functions that are now available that were not when the ArrayUtil was writen.

- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
Thank you
I believe, there is still a big gap between the methods provided by ArrayUtil and ES5. ES5 introduces indexOf, contains, concat, filter, reduce, map, and a few other good methods but for other functionalities like converting Java object to javascript array[convertArray], intersection, union, differences we need to build custom logic by leveraging functions like filter, reduce, etc. Please let me know if I am missing the cool features of ES5.
Have a nice one 🙂
Thanks & Regards,
Sharjeel

- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
I agree, I was just pointing out that with the introduction of there modified version of ES5 that not all of the ArrayUtil is needed anymore.

- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
Sounds Good, and thanks again for adding the valuable information! 🙂

- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
Updated!