Find your people. Pick a challenge. Ship something real. The CreatorCon Hackathon is coming to the Community Pavilion for one epic night. Every skill level, every role welcome. Join us on May 5th and learn more here.

Sai Kumar B
Mega Sage

I always wonder, Is there any simple utility in ServiceNow to fetch the list of hierarchy/table extensions? 🤔

Well, By OOB we have TableUtils(). It contains multiple functions to perform on a table.

I will walk you through a few interesting functions.

getTables()  :  Returns list of table names in the table parent hierarchy

var hierarchyList = new TableUtils('cmdb_ci_server').getTables();
gs.print(hierarchyList); //Displays all its parent hierarchy along with the input

find_real_file.png

getTableExtensions() :  Returns list of table extensions

 
var hierarchyList = new TableUtils('cmdb_ci_server').getTableExtensions();
gs.print(hierarchyList); //Displays all its extensions

find_real_file.png

getAllExtensions()  : Returns list of table extensions including base table 

var hierarchyList = new TableUtils('cmdb_ci_server').getAllExtensions();
gs.print(hierarchyList); //Displays all its extensions including base 

find_real_file.png

Now, Let's iterate over the output. The output was displayed as an array list however, it's not an array 😐

It's an object and we have to convert it into an array as below 

var hierarchyList = new TableUtils('cmdb_ci_server').getTables();
gs.print(hierarchyList);

var arr = hierarchyList.toArray(); //Convert the output into an array 
for(var i=0; i<arr.length; i++){ //Iteration
    gs.print(arr[i]);
}

find_real_file.png

I hope you like my article 😊

Kindly, Post comments if any corrections are required 

Regards,
Sai Kumar

Comments
Sohail Khilji
Kilo Patron

good one !

David Casper
Tera Guru

Great post, first found this a long time ago. 

However, I just had to reuse the getTableExtensions method and noticed some interesting things. 

 

1) When using the getTableExtensions method, I had to use .size() rather than .length in order to read the elements within the array. Any ideas why? Assuming the type of array it's creating and I don't remember having to do this before. 

2) When looking at a specific element in the array I'm unable to use array[i] I have to use array.get(i)

 

Really would like to understand what's going on behind the scenes here so I know why this was required. Looks like this might be creating a Java array? Thanks!

jacobperry-
Tera Explorer

@David Casper

 

1) When using the getTableExtensions method, I had to use .size() rather than .length in order to read the elements within the array. Any ideas why? Assuming the type of array it's creating and I don't remember having to do this before.

2) When looking at a specific element in the array I'm unable to use array[i] I have to use array.get(i)

 

You are correct, it's returning a Java Object, specifically java.util.ArrayList. Whenever you encounter this behaviour, you can wrap the output in j2js() convert the Java Object into an equivalent Javascript object. 

 

new TableUtils('cmdb_ci_server').getTables() //Returns Java ArrayList
j2js(new TableUtils('cmdb_ci_server').getTables()) //Returns Javascript Array

 

If you look at the TableUtils script include, you will see these methods invoke the respective GlideDBObjectManager methods. 

Version history
Last update:
‎12-27-2021 12:19 AM
Updated by: