- Post History
- Subscribe to RSS Feed
- Mark as New
- Mark as Read
- Bookmark
- Subscribe
- Printer Friendly Page
- Report Inappropriate Content
on 02-02-2020 06:42 PM
As a caveat, what I'm about to share would likely be considered a customization. Even though this approach does not technically alter any core code, this does alter core functionality, so use with the understanding that if there are any issues ServiceNow Support would suggest discontinuing use as the solution. That being said - I've used this approach with no issues across several versions.
As I've mentioned in a previous article GlideRecord is one of, if not the most, powerful tools at your disposal as a developer in ServiceNow. Learning how to use GlideRecord effectively makes it possible to craft advanced solutions to meet complex business requirements. That being said I've occasionally found GlideRecord lacking in certain features that, while clearly possible to duplicate, I wish were built in for the sake of convenience. After spending years simply accepting the fact I decided to see if I could find a solution and, while it took some research along with trial and error, it turns out to be surprisingly easy to implement.
As an example, two tasks I find myself constantly writing extra code to accomplish would be logging a value (for testing) and writing a list of values to an array. While these are just examples, the same concept can be use to add whatever functionality you find useful.
*Special thanks to Drew for suggesting a much cleaner and more efficient method for evaluating dot walked values!
In this example create a script include named customGlideUtils
GlideRecord.prototype.logValue = function(field) {
var fa = field.split(".");
var e = this[fa[0]];
for(var i = 1; i < fa.length; i++){
e = e[fa[i]];
}
gs.info(e.getValue());
};
GlideRecord.prototype.valueArray = function(field) {
var valueList = [];
while(this.next()) {
var fa = field.split(".");
var e = this[fa[0]];
for(var i = 1; i < fa.length; i++){
e = e[fa[i]];
}
valueList.push(e.getValue());
}
return valueList;
};
You can then invoke the additional functionality in the following manner:
//This line makes the custom functions available
gs.include('customGlideUtils');
var incidents = new GlideRecord('incident');
incidents.addActiveQuery();
incidents.setLimit(10);
incidents.query();
while(incidents.next()) {
//This logs each caller)id.name to the log individually
incidents.logValue('caller_id.name');
}
//This line makes the custom functions available
gs.include('customGlideUtils');
var incidents = new GlideRecord('incident');
incidents.addActiveQuery();
incidents.setLimit(10);
incidents.query();
//This will cycle through the list and return an array of caller_id.name
var nameArr = incidents.valueArray('caller_id.name');
gs.info(nameArr);
While the first example .logValue() doesn't really save much in the way of code, the second example .valueArray() eliminates the need to constantly include the while-loop and push routine to achieve the desired result. The real benefit I find from this approach is that it keeps all of the functionality within the same class, which means you don't have to pass objects back and forth, keeping track of changes etc.
This same methodology can of course be used to enhance any class, not just GlideRecord, and while I certainly don't suggest over using this approach, it can be a viable way to add some quality of life improvements to existing functionality without resorting to the effort of writing fully custom class.
If you found this article helpful or useful, please be kind and click appropriately. If you found it really useful, you could always use the 3-dot menu to bookmark it for later!
Michael Jones - Proud member of the CloudPires team!
- 949 Views
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
Just a thought on how you could do the above code to get a dot-walked value with out having to "hard code" a level limit.
var fa = field.split(".");
var e = this[fa[0]];
for(var i = 1; i < fa.length; i++){
e = e[fa[i]];
}
gs.info(e.getValue());
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
Thank you for taking the time to make an excellent suggestion! I'll admit I tried figuring something like this out, but could never quite get it to work the way I expected. I love it when I share something and end up learning something new as a result.
I added a credit for you in the post above. 🙂