Get a list of all parents of a table.

toddemaurer
Tera Contributor

My scripts need to know if a table is a decentent of another table.  Not jsut parent and child.  For example:

  • Yes cmdb_ci_win_server is a decendant of cmdb_ci_hardware.
  • No cmdb_ci_rack is not a decendant of cmdb_ci_hardware.

I found the following Link: https://developer.servicenow.com/app.do#!/api_doc?v=london&id=r_CMDBU-getTables_S

It talks about CMDBUtil. Reading it there is a getTables.  I would think this would give me what I need but I can't may it work.

the eample for static works for me:

var output = SNC.CMDBUtil.getAllChildrenOfAsCommaList('cmdb_ci_computer');
gs.print(output);

But

var output = SNC.CMDBUtil.getTables('cmdb_ci_computer');
gs.print(output.to_string());

 

results in the output of undefined. (I added the .to_string because the return of .getTables is an array.

 

So either I need advice on getting .getTables to work or a different method.

7 REPLIES 7

ccajohnson
Kilo Sage

Since the SNC.CMDBUtil script include is hidden, we cannot look to see what the script contains.

All tables are defined in the Tables [sys_db_object] table. Those that are extended from another table (i.e.; have a parent) store that table in the Extends table [super_class] field. Here is a simple background script that takes the table name and returns the Table's name, sys_id, and Label:

 var parObj = getParentTableDetails('cmdb_ci_computer');
gs.info('Parent name: ' + parObj.name);
gs.info('Parent sysID: ' + parObj.id);
gs.info('Parent Label: ' + parObj.label);

function getParentTableDetails(cTableName) {
    var tblQry = new GlideRecord('sys_db_object');
    tblQry.get('name', cTableName);
    return {
        name: tblQry.super_class.name,
        id: tblQry.super_class,
        label: tblQry.super_class.label
    };
}

Feel free to leverage the script and fold it in to your own development.

Thanks, I was aware of the possibility of doing this, this way, but was hoping that serviceNow had an function that already did this OOB (Out Of the Box) rather than reinvent the wheel.

The function I will need to write will need to be recursive as I don't want to stop at the parent, but keep looking up the tree all the way to the root.

Jon Barnes
Kilo Sage

GlideDBObjectManager.get().getTables(tableName) returns the table name and all tables in the hierarchy as an array. this should do the trick

BTW, there's another way...

new GlideRecordUtil().getTables(tableName);

Looks to give the exact same results as GlideDBObjectManager.get().getTables(tableName).

Both work, but GlideRecordUtil is documented, while GlideDBObjectManager doesn't seem to be.  I did find some old documentation at https://old.wiki/index.php/Scoped_GlideTableHierarchy_API_Reference, and it does rather more than GlideRecordUtil.

But for getting the table hierarchy, GlideRecordUtil() is probably safer.