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

Just found another way...

new TableUtils(tableName).getTables();

Which again looks to do exactly the same thing.  Documented here.

Note: these functions return a Java ArrayList, not a Javascript array. You can convert it to a Javascript array using j2js(), documented here

A-N
Tera Expert

Hi Tod,

Run this

Here's one way to do it

in the Scripts - Background

P:S :: Change the variable is_child and parent 

var is_child =  "cmdb_ci_win_server";
var parent = "cmdb_ci_hardware";
var flag = 0;
var answer="";
var l = SNC.CMDBUtil.getAllChildrenOfAsCommaList(parent);
//answer = answer + l;
answer +=  l;
var array = answer.split(',');
var gr = new GlideRecord("sys_db_object");
array.forEach(function (arrayItem) {
(gr.get(arrayItem)  && (gr.name == is_child)) ? flag=1:'';
});
flag ? gs.print('Yes ' +is_child + ' is a descendant of ' +parent):gs.print('No ' +is_child + ' is NOT a descendant of ' +parent);
Output:
*** Script: Yes cmdb_ci_win_server is a descendant of cmdb_ci_hardware

When is_child is 'cmdb_ci_rack'
var is_child =  "cmdb_ci_rack";
var parent = "cmdb_ci_hardware";
var flag = 0;
var answer="";
var l = SNC.CMDBUtil.getAllChildrenOfAsCommaList(parent);
//answer = answer + l;
answer +=  l;
var array = answer.split(',');
var gr = new GlideRecord("sys_db_object");
array.forEach(function (arrayItem) {
(gr.get(arrayItem)  && (gr.name == is_child)) ? flag=1:'';
});
flag ? gs.print('Yes ' +is_child + ' is a descendant of ' +parent):gs.print('No ' +is_child + ' is NOT a descendant of ' +parent);

output :

*** Script: No cmdb_ci_rack is NOT a descendant of cmdb_ci_hardware

 

aaronradford
Tera Contributor

Just use recursion.

 

function getsuperhierarchy(tablename, arr) {

if (typeof arr === 'undefined') arr = [tablename];
else arr.push(tablename);

if (gs.nil(tablename)) return arr;

var tablegr = new GlideRecord('sys_db_object');
tablegr.addQuery('name', tablename);
tablegr.query();

if (!tablegr.next()) return arr;

if (gs.nil(tablegr.super_class)) return arr;

var superval = tablegr.super_class.getRefRecord().getValue('name');

return getsuperhierarchy(superval, arr);

}