Get a list of all parents of a table.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-28-2019 02:12 PM
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.
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-14-2020 08:08 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-29-2019 01:28 PM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-01-2020 05:47 PM
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);
}