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 INAUGURAL CLOUDSHERPAS SCRIPTING 101 BLOG ARTICLE FROM February 4, 2015.

____________________________________________________________________________

 

Welcome to our new blog series, ServiceNow Scripting 101!  I have been writing this type of article under the ServiceNow Admin 101 series, but they really needed their own platform. Don’t worry though: the Admin 101 series will still continue, but it will contain more Admin-ish articles from this point forward. Enjoy!

 

Recently, our Web Service Integration team needed a method for looking up all of the fields belonging to a table and then displaying those fields in a pick list. This needed to be done for ALL of the fields in the table hierarchy. 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?

  • 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

 

1. Field: Table - a choice list of table names.

For example:

2. Field:  Field - A lookup / reference field referencing Dictionary Entry and controlled by the Table field choice.

 

3. The format should look something like this:

 

 

Next, the Script Include will need 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.

Here is the Script Include:

  1. Name: DictionaryUtils
  2. Description:  Helper methods for Dictionary references
  3. Script:

 

Here we would place our new recursive method:

 

var tableList = ''; // resultant lookup string that would be returned


var DictionaryUtils = Class.create();
DictionaryUtils.prototype = {

  initialize: function() {
  },


  //Recursive method for retrieving all Tables in the Hierarchy for a given table.
  //USAGE: new DictionaryUtils().getTableNameList('cmdb_ci_computer');
  //RETURNED: name='cmdb_ci_computer'^ORname='cmdb_ci_hardware'^ORname='cmdb_ci'
  getTableNameList : function(tableName) {

      // construct our lookup string
      tableList += "name=" + tableName + "^OR";

      // lookup the given table to retrieve our next super class name
      var tableLookup = GlideRecord('sys_db_object');
      tableLookup.addQuery('name', tableName);
      tableLookup.query();


      // 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 + '');
      }

      // peel off the last ^OR on the end of the lookup 
      // string and return it to the calling method.
      return tableList.substring(0, tableList.length - 3) + '^element!=NULL';
  },

  type: 'DictionaryUtils'
};

 

 

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 original form.

 

  1. Reference: Dictionary Entry
  2. 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. 

 

 

Test

  1. Pick a table. I chose Incident.
  2. Click on the “Fields” magnifying glass.
  3. 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 Advanced Reference Qualifiers, check out this Wiki article.

 

For more on JavaScript recursion, check out these resources:

 

Steven Bell

 

accenture technology logo.pngsn-community-mvp.png

 

For a list of all of my articles:  Community Code Snippets: Articles List to Date

 

Please Share, Like, Bookmark, Mark Helpful, or Comment this blog if you've found it helpful or insightful.

 

Also, if you are not already, I would like to encourage you to become a member of our blog!

 


Click for More Expert Blogs and also Find Expert Events!