
- 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.
While pondering all the modifications I had made to date I realized that I still had not arrived at all of my targeted functionality. I still wanted the same ability as I would get in a true SQL SELECT statement. Namely to specify which fields were returned. In other words proper SQL SELECT capability. While SELECT * was fine I wanted SELECT field1, field2, ..., fieldn! When I originally envisioned this functionality GlideQuery didn't even exist. If you are interested in how ServiceNow implemented GlideQuery on top of GlideRecord it is using something similar to the method I describe here.
I played around trying to get a .addSelect(field1, etc.);, but was unable to come up with something I liked; let alone worked right!
I settled for something not as nice, but that still worked. I ended up adding a new prototype method to my GlideRecordExtensions Script Include to do the deed.
This is the format:
var <<object array>> = <<glideRecord>>.select('field1, field2, ..., fieldn');
The select method will return an object array containing each record, but with only the fields specified.
What follows is how to add this functionality.
Lab - Array.indexOf and the JavaScriptExtended class.
When I originally wrote this article this functionality did not exist in the ServiceNow platform. It has since been added and it is no longer necessary to implement this prototype. If you are interested you may find the prototype polyfill here:
1. Navigate to Scripts - Background and run the following test to verify the functionality actually exists in the language base.
var fieldList = ['cmdb_ci_server', 'cmdb_ci_moose', 'cmdb_ci'];
var findField = fieldList.indexOf('cmdb_ci');
gs.info(findField); // result should be 2
Results:
*** Script: 2
If that doesn't work make sure you are on a recent version of ServiceNow. Now we are ready to extend GlideRecord!
Lab: Add Select to GlideRecordExtension
With this lab we will be adding our new select method to the existing GlideRecordExtension Script Include.
1. Navigate to Script Includes and edit the GlideRecordExtension Script Include.
2. You will notice that I added the following new function to the end of the Script Include:
GlideRecord.prototype.select = function(selectFields) {
this.selectFields = []; // initialize our global array
this.isSelect = true; // turn on the filter
this.selectFields = selectFields;
var selectList = this.toObjectList();
this.isSelect = false; // turn off the filter
return selectList;
};
NOTE: It is considered a best practice to initialize your this variables (or any variables for that matter).
NOTE: I have introduced a new wrinkle in functionality. If you add a variable to the "this" object in a prototype method it becomes a global variable to the GlideRecord object (or any object where this is done) when the new function (.select) is called the first time. When we assign a value to such a variable; that value is now available to all following function calls. So, in this case isSelect and selectFields will be available later to other functions called inside of GlideRecordExtended.
3. I then modified the GlideRecord.prototype.toObject function:
GlideRecord.prototype.toObject = function(recordToPackage) {
var packageToSend = {};
for (var property in recordToPackage) {
try {
if ((this.isSelect && this.selectFields.indexOf(property) > -1) || !this.isSelect) {
packageToSend[property] = recordToPackage[property].getDisplayValue();
}
}
catch(err){} // eat any error
}
return packageToSend;
};
This new "if" check looks to see if we are doing a "select" AND if the specific field is one we are wanting, otherwise retain our old functionality.
Now we are ready to test our new select function!
Lab - Test the Gliderecord.select Function
1. Navigate to Scripts - Background.
2. Paste in the following script:
gs.include('GlideRecordExtensions'); // include our library of new functions
// this is our test recordset of 10 records
var incidents = new GlideRecord('incident');
incidents.addActiveQuery();
incidents.setLimit(10);
incidents.query();
// Execute our new select function. Ask for just the sys_id and number fields.
var incidentList = incidents.select('sys_id, number');
// Loop through and print out our resultant object array
for (var i=0; i < incidentList.length; i++) {
gs.info('{0} - {1}, {2}',
incidentList[i].number,
incidentList[i].sys_id,
incidentList[i].assigned_to /* not in select list */);
}
gs.info(' ');
// Execute the toObjectList function to retrieve ALL fields
var incidentFullList = incidents.toObjectList();
// Loop through and print out our resultant object array
for (var i=0; i < incidentFullList.length; i++) {
gs.info('{0} - {1}, {2}',
incidentFullList[i].sys_id,
incidentFullList[i].number,
incidentFullList[i].assigned_to);
}
incidents = null; // Drop our heavy object from memory
NOTE: You will notice I added a line to the end of the test. While not really needed (JavaScript does regular garbage collection at the end of execution) this shows a good practice in how to get rid of the massive GlideRecord object from memory when it is no longer needed. See the following article for more on memory management.
3. Click the Run script button.
Your results should look something like this:
*** Script: INC0000002 - 9d385017c611228701d22104cc95c371, undefined
*** Script: INC0000003 - e8caedcbc0a80164017df472f39eaed1, undefined
*** Script: INC0000007 - 8d6353eac0a8016400d8a125ca14fc1f, undefined
*** Script: INC0000015 - 46e2fee9a9fe19810049b49dee0daf58, undefined
*** Script: INC0000016 - 46e3e949a9fe19810069b824ba2c761a, undefined
*** Script: INC0000017 - 46e482d9a9fe198101d3e3f3e2a14459, undefined
*** Script: INC0000018 - 46e57642a9fe1981000b96a5dca501ff, undefined
*** Script: INC0000019 - 46e8219ba9fe1981013806b6e04fed06, undefined
*** Script: INC0000020 - 46edaa6aa9fe198101b9d14ced16619f, undefined
*** Script: INC0000025 - 46f09e75a9fe198100f4ffd8d366d17b, undefined
*** Script:
*** Script: 9d385017c611228701d22104cc95c371 - INC0000002, Howard Johnson (howard.johnson)
*** Script: e8caedcbc0a80164017df472f39eaed1 - INC0000003, Beth Anglin (beth.anglin)
*** Script: 8d6353eac0a8016400d8a125ca14fc1f - INC0000007, David Loo (david.loo)
*** Script: 46e2fee9a9fe19810049b49dee0daf58 - INC0000015, Don Goodliffe (don.goodliffe)
*** Script: 46e3e949a9fe19810069b824ba2c761a - INC0000016, ITIL User (itil)
*** Script: 46e482d9a9fe198101d3e3f3e2a14459 - INC0000017, Fred Luddy (fred.luddy)
*** Script: 46e57642a9fe1981000b96a5dca501ff - INC0000018, ITIL User (itil)
*** Script: 46e8219ba9fe1981013806b6e04fed06 - INC0000019, Bud Richman (bud.richman)
*** Script: 46edaa6aa9fe198101b9d14ced16619f - INC0000020, ITIL User (itil)
*** Script: 46f09e75a9fe198100f4ffd8d366d17b - INC0000025, ITIL User (itil)
The first part of the test verifies that our new function is working correctly. The select method goes out and only retrieves the specified fields and nothing else. Since assigned_to was not in our select fields list then it was not retrieved and returns "org.mozilla.javascript.Undefined".
The second part of the test verifies we have not broken our old functionality and that everything is returned. The assigned_to field will be properly filled in.
End of testing!
There you have it! Not a perfect solution, but the best we can do for the moment (unless you want to go to GlideQuery).
BTW, if you are interested in my comparison of GlideRecord with GlideQuery you can find those articles here:
GlideQuery vs. GlideRecord: A Comparison - Part 1
GlideQuery vs. GlideRecord: A Comparison - Part 2
Enjoy!
Steven Bell.
If you find this article helps you, don't forget to log in and mark it as "Helpful"!
Originally published on: 10-26-2015 10:24 AM
I updated the code and brought the article into alignment with my new formatting standard.
- 973 Views
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.