
- Post History
- Subscribe to RSS Feed
- Mark as New
- Mark as Read
- Bookmark
- Subscribe
- Printer Friendly Page
- Report Inappropriate Content
07-26-2018 02:41 PM - edited 11-10-2023 06:32 AM
NOTE: MY POSTINGS REFLECT MY OWN VIEWS AND DO NOT NECESSARILY REPRESENT THE VIEWS OF MY EMPLOYER, ACCENTURE.
DIFFICULTY LEVEL: INTERMEDIATE
Assumes having taken the class SSNF and has good intermediate level of knowledge and/or familiarity with Scripting in ServiceNow.
Here is a nifty method for searching through an object array and bringing back a specific object based on what is in the field. he nicety of this is that you don't have to loop through the array and do the comparison yourself. It is handled using the JavaScript array.filter method!
1. Navigate to Scripts - Background
2. Copy and past the following script into the script text box (you can read through the comments to see what is going on):
var location = 'SB: FindObject Test';
var myObjectList = [];
try {
// Add three objects to the object list.
var myObject = {};
myObject.name = 'Jane Jetson';
myObject.title = 'Family Manager';
myObject.location = 'Austin';
myObject.key = (myObject.name + '|' + myObject.title + '|' + myObject.location).toLowerCase() + '';
myObjectList.push(myObject);
myObject = {};
myObject.name = 'George Jetson';
myObject.title = 'Building Architect';
myObject.location = 'Dallas';
myObject.key = (myObject.name + '|' + myObject.title + '|' + myObject.location).toLowerCase() + '';
myObjectList.push(myObject);
myObject = {};
myObject.name = 'Wilma Flintstone';
myObject.title = 'Boss';
myObject.location = 'Ft. Worth';
myObject.key = (myObject.name + '|' + myObject.title + '|' + myObject.location).toLowerCase() + '';
myObjectList.push(myObject);
// now search for a specific object based on the title field!
// to really get specific go for the key field
var myFoundObject = findObject('Building Architect', myObjectList, 'title');
gs.info('---> [{2}] Name: {0}, Title: {1}\n', [myFoundObject.name, myFoundObject.title, location]);
// btw, since the found object is a pointer to the actual object in the object
// array you can modify it and it modifies the original.
myFoundObject.title = 'Supervisor';
// now loop through the objects and lets see if the title for George changed
for (var item in myObjectList) {
gs.info('---> [{2}] Name: {0}, Title: {1}',
[myObjectList[item].name,
myObjectList[item].title,
location]);
}
}
catch(err) {
gs.error('---> [{1}] ERROR: {0}', [err, location]);
}
// use the builtin filter function to locate an array element by a value in one
// of its object fields. BTW, when we finally move to ECMA 6 this becomes a one-liner!
function findObject(source, targetList, field) {
return targetList.filter(function ( obj ) {
return obj[field] === source;
})[0];
}
3. Click on Run script and make sure you in the Global scope.
4. Your results will look like this:
*** Script: ---> [SB: FindObject Test] Name: George Jetson, Title: Building Architect
*** Script: ---> [SB: FindObject Test] Name: Jane Jetson, Title: Family Manager
*** Script: ---> [SB: FindObject Test] Name: George Jetson, Title: Supervisor
*** Script: ---> [SB: FindObject Test] Name: Wilma Flintstone, Title: Boss
I ended up adding the findObject and other functions to a Function Script Include Library which I will write about in another article.
Enjoy!
Steven Bell.
If you find this article helps you, don't forget to log in and mark it as "Helpful"!
Originally published on: 7-26-2018 06:41 AM
I updated the code and brought the article into alignment with my new formatting standard.
- 1,082 Views