GlideTable Hierarchy is a ServiceNow Server scoped API which provides information about the Table hierarchy and relationships. We have TableUtils API which can be used to retrieve the hierarchy of tables in ServiceNow.
So How actually the Table Hierarchy API is useful?
TableUtils is limited to the global scope in ServiceNow and it cannot be accessed from scoped application. So there is either of ways to achieve this solution:
- Copy TableUtils inside the scoped application.
- writing logic for GlideRecord the “sys_db_object” table.
This solution implementation might take some additional effort for a developer. So here TableHierarchy API can be very useful to bring our efforts down and easy in implementation. To get the hierarchy of any table in scoped applications. The below methods will be useful.
Method Summary:
Method | Definition |
GlideTableHierarchy(String tableName) | Instantiates a GlideTableHierarchy object |
getName() | This method returns the table’s name |
getTables() | This method returns an array of strings containing table names in the hierarchy |
getTableExtensions() | This method returns an array containing the table names that extend the current table |
getAllExtensions() | This method returns an array containing the table names that extend the current table and includes the current table |
getHierarchy() | This method returns an array of all classes in the hierarchy of the current table |
getRoot() | This method returns the top-level class in the hierarchy |
getBase() | This method returns the parent class |
isBaseClass() | This method returns a boolean value, true if this is the base class |
isSoloClass() | This method returns a boolean value, true if this is not part of any hierarchy |
hasExtensions() | This method returns a boolean value, true if the table has any child classes |
In this Blog, We will drive through a few methods of this API with reference to the CMDB table hierarchy. For that we can use this API as follows:
API Definition: getTableExtensions() returns an array containing the table names that extend the current table.
Note: Current table will not be part of the return object. If you want to include the current table in the return object use getAllExtensions() method.
To fetch all the child tables(hierarchy) for the cmdb_ci_computer table. We can use this method as explained below in the example.
Scoped | Global |
var hierarchy=new GlideTableHierarchy (‘cmdb_ci_computer’);var tables=hierarchy.getTableExtensions();gs.info(tables); | var hierarchy= new TableUtils (“cmdb_ci_computer”);var tables=hierarchy.getTableExtensions();gs.info(tables); |
Output:
cmdb_ci_ucs_blade, cmdb_ci_server, cmdb_ci_osx_server, cmdb_ci_mainframe, cmdb_ci_datapower_server, cmdb_ci_lb, cmdb_ci_lb_bigip, cmdb_ci_lb_a10, cmdb_ci_lb_cisco_csm, cmdb_ci_lb_alteon, cmdb_ci_lb_f5_gtm, cmdb_ci_lb_cisco_gss, cmdb_ci_lb_isa, cmdb_ci_lb_cisco_css, cmdb_ci_lb_ace, cmdb_ci_lb_radware, cmdb_ci_lb_network, cmdb_ci_lb_netscaler, cmdb_ci_lb_f5_ltm, cmdb_ci_ibm_zos_server, cmdb_ci_virtualization_server, cmdb_ci_vcenter_server_obj, cmdb_ci_esx_server, cmdb_ci_hyper_v_server, cmdb_ci_storage_server, cmdb_ci_win_server, cmdb_ci_linux_server, cmdb_ci_storage_node_element, cmdb_ci_isam_server, cmdb_ci_mainframe_lpar, cmdb_ci_server_hardware, cmdb_ci_tape_server, cmdb_ci_net_app_server, cmdb_ci_chassis_server, cmdb_ci_netware_server, cmdb_ci_unix_server, cmdb_ci_aix_server, cmdb_ci_solaris_server, cmdb_ci_hpux_server, cmdb_ci_cim_server, cmdb_ci_storage_switch, cmdb_ci_mainframe_hardware, cmdb_ci_pc_hardware, cmdb_ci_ucs_rack_uniAPI Definition: getHierarchy() returns an array of all classes in the hierarchy of the current table.
Scoped | Global |
var hierarchy=new GlideTableHierarchy(‘cmdb_ci_computer’);var tables=hierarchy.getHierarchy();gs.info(tables); | var hierarchy= new TableUtils(“cmdb_ci_computer”);var tables=hierarchy.getHierarchy();gs.log(tables); |
Output:
cmdb_ci_computer, cmdb_ci_hardware, cmdb_ci, cmdb, cmdb_ci_ucs_blade, cmdb_ci_server, cmdb_ci_osx_server, cmdb_ci_mainframe, cmdb_ci_datapower_server, cmdb_ci_lb, cmdb_ci_lb_bigip, cmdb_ci_lb_a10, cmdb_ci_lb_cisco_csm, cmdb_ci_lb_alteon, cmdb_ci_lb_f5_gtm, cmdb_ci_lb_cisco_gss, cmdb_ci_lb_isa, cmdb_ci_lb_cisco_css, cmdb_ci_lb_ace, cmdb_ci_lb_radware, cmdb_ci_lb_network, cmdb_ci_lb_netscaler, cmdb_ci_lb_f5_ltm, cmdb_ci_ibm_zos_server, cmdb_ci_virtualization_server, cmdb_ci_vcenter_server_obj, cmdb_ci_esx_server, cmdb_ci_hyper_v_server, cmdb_ci_storage_server, cmdb_ci_win_server, cmdb_ci_linux_server, cmdb_ci_storage_node_element, cmdb_ci_isam_server, cmdb_ci_mainframe_lpar, cmdb_ci_server_hardware, cmdb_ci_tape_server, cmdb_ci_net_app_server, cmdb_ci_chassis_server, cmdb_ci_netware_server, cmdb_ci_unix_server, cmdb_ci_aix_server, cmdb_ci_solaris_server, cmdb_ci_hpux_server, cmdb_ci_cim_server, cmdb_ci_storage_switch, cmdb_ci_mainframe_hardware, cmdb_ci_pc_hardware, cmdb_ci_ucs_rack_unit
API Definition: getRoot() returns the top-level class in the hierarchy.
For Global, a Similar method is getAbsoluteBase() which returns the base table name from which the table was extended.
Note: For any table under the cmdb_ci hierarchy, this method returns cmdb_ci and not CMDB, which is the actual base table.
Scoped | Global |
var hierarchy=new GlideTableHierarchy(‘cmdb_ci_computer’);var tables=hierarchy.getRoot();gs.info(tables); | No similar Method available |
Output:
cmdb
API Definition: getBase() returns the parent class which current class is extending like task(return) for incident(argument).
Scoped | Global |
var hierarchy=new GlideTableHierarchy(‘cmdb_ci_computer’);var tables=hierarchy.getBase();gs.info(tables); | No similar Method available |
Output:
Cmdb_ci_hardware
Thanks You for Readings
For More Contents on Service Now go to Aelum Consulting Blogs