- Post History
- Subscribe to RSS Feed
- Mark as New
- Mark as Read
- Bookmark
- Subscribe
- Printer Friendly Page
- Report Inappropriate Content
on 12-27-2021 12:19 AM
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
getTableExtensions() : Returns list of table extensions
var hierarchyList = new TableUtils('cmdb_ci_server').getTableExtensions();
gs.print(hierarchyList); //Displays all its extensions
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
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]);
}
I hope you like my article 😊
Kindly, Post comments if any corrections are required
Regards,
Sai Kumar
- 2,905 Views
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
good one !

- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
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!
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
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.