
- 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: INTERMEDIATE
Assumes having taken the class SSNF and has good intermediate level knowledge and/or familiarity of Scripting in ServiceNow.
One of our clients needed a method to be able to display and choose from a list of all of the fields, belonging to a table, in a pick list. This needed to be done for ALL of the fields in the table hierarchy (example: task > incident or cmdb_ci_computer > cmdb_ci_hardware > cmdb_ci > cmdb). In other words, what was needed was a smart Dictionary Entry of Dictionary Entries for a given table! While the solution turned out to be a little tricky, it wasn't too bad...honest!
Requirements
What was needed?
- Where? Custom Table Form.
- A short list of tables to drive a lookup of a list of available fields.
- A lookup that could present the user with all fields available in the table hierarchy.
- A way to determine the hierarchy table list in order to build a list of available fields.
Solution Example / Lab
For the purposes of demonstrating the techniques I want to convey we will be creating a custom table with two fields. The first field will contain a short list of tables. The second field will do a field lookup, on the chosen table, that will include all inherited fields.
1, Create a custom table that will contain two fields.
a. Lable: MyCustomRecursive
b. Name: u_mycustomrecursive (this will automatically appear)
c. Fields:
i. Table
ii. Field
2. New Field: Table - a choice list of table names.
a. Label: Table
b. Type: Choice
c. Add the following choices:
2. New Field: Field - A lookup / reference field referencing Dictionary Entry and controlled by the Table field choice.
a. Label: Field
b. Type: Reference
c. Reference: Dictionary Entry
d. We will add the reference qualifier later.
The format on the table form should look something like this:
We will need a function to traverse a given table's hierarchy. This type of method is best done with a recursive routine. Incidentally I decided to do the groundwork for future Dictionary methods like this by creating a Dictionary Utility library. It is a good practice to attempt to group similar functions into a library format. You will find several examples of this with out-of-the-box Script Includes; just search for "Util" in the list view.
We will create the beginnings of a library with a new Script Include:
- Name: MyDictionaryUtils
- Description: Helper methods for Dictionary references
- Script:
Here we would place our new recursive method:
var MyDictionaryUtils = Class.create();
MyDictionaryUtils.prototype = {
initialize: function() {},
//Recursive method for retrieving all Tables in the Hierarchy for a given table.
//USAGE: new MyDictionaryUtils().getTableNameList('cmdb_ci_computer');
//RETURNED: name='cmdb_ci_computer'^ORname='cmdb_ci_hardware'^ORname='cmdb_ci'
getTableNameList : function(tableName) {
this.tableList += 'name=' + tableName + '^OR';
// construct our lookup string
// lookup the given table to retrieve our next super class name
var tableLookup = GlideRecord('sys_db_object');
tableLookup.addQuery('name', tableName);
tableLookup.query();
try {
// chase each super class (i.e. parent) up the chain.
// the topmost parent will NOT have a parent
if (tableLookup.next() && tableName != '') {
this.getTableNameList(tableLookup.super_class.name + '');
}
}
catch(err) {
gs.error('---> [{1}] ERROR: {0}', [err, this.type + '.getNameList']);
}
// peel off the last ^OR on the end of the lookup
// string and return it to the calling method.
return this.tableList.substring(0, this.tableList.length - 3) + '^element!=NULL';
},
type: 'MyDictionaryUtils',
tableList: ''
};
So, the magic is to work "up" the inheritance chain via the super_class reference field and build the list of tables accordingly.
The Script Include should look something like this:
NOTE: A good practice is to test this script from Scripts - Background prior to implementing in your Dictionary Entry.
Finally, modify the Dictionary Entry of the lookup/reference field on the form.
NOTE: You may need to flip to Advanced View for this.
- Reference: Dictionary Entry (already filled in)
- Use Reference Qualifier: Advanced
- Reference qual: javascript:new DictionaryUtils().getTableNameList(current.u_table);
Where "u_table" is the table field that where we get our table name.
This will call the new Script Include method and construct an "OR" string of tables representing the table hierarchy for current.u_table.
This in turn will cause the lookup to pull up a list of fields based on those tables.
4. Click on the Update button to save your work.
Test
- Navigate to u_mycustomrecursive.list
- Click on the New button.
- From the table field pick a table. I chose Incident.
- Click on the "Fields" magnifying glass.
- Modify the list view (right-click / Personalize / List Layout) and your result should look something like this:
And that's all there is to it!
Learn More
If you're interested in finding out more about Reference Qualifiers, check out the docs site:
Configure reference qualifiers (servicenow.com)
For more on JavaScript recursion, check out these resources:
- JavaScript Recursion (with Examples) (programiz.com)
- JavaScript Recursive Function By Examples (javascripttutorial.net)
BTW, if you are interested in taking this to the next step I added this utility function to my utilities library: Community Code Snippets: Useful Utilities Library
Enjoy!
Steven Bell.
If you find this article helps you, don't forget to log in and mark it as "Helpful"!
NOTE: ON APRIL 1, 2016 ACCENTURE COMPLETED THE ACQUISITION PROCESS OF CLOUDSHERPAS. AT THAT TIME THE CLOUDSHERPAS BLOG SITES WERE DOWNED FOREVER.
THIS IS THE RE-PUBLICATION OF MY ARTICLE FROM February 4th, 2015 ON THE CLOUDSHERPAS SERVICENOW ADMIN 101 BLOG.
Originally published on: 05-25-2016 07:29 AM
I updated the code and brought the article into alignment with my new formatting standard.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.