CMDB hierarchy in script

chidanandadhath
Kilo Guru
I want to populate CMDB hierarchy for example if cmdb_ci_computer is the CI Class Row, then the end of the row should look something like this cmdb: cmdb_ci : cmdb_ci_hardware: cmdb_ci_computer
basically want to populate hierarchy of CI class
1 ACCEPTED SOLUTION

Jeah, i forgot one part. The below one should work:

function getParentTables(childTableName)
{
	var parentTables = [];
	var table = new GlideRecord('sys_db_object');
	table.addQuery('name', childTableName);
	table.addNotNullQuery('super_class');
	table.query();
	if(table.next())
		{
		var parentTableName = table.super_class.name;
		parentTables.push(parentTableName);
		
		// get the parents of the parent and appen it
		parentTables = parentTables.concat(getParentTables(parentTableName));
	}
	return parentTables;
}

Greetings

Fabian

View solution in original post

5 REPLIES 5

Fabian Kunzke
Kilo Sage
Kilo Sage

Hello,

I don't quite understand what the issue is. Do you need help with the scripting? Do you just need the information if this is possible?

It is in fact possible to do this. A functioni as follows will return you all names of the parent table structure. 

function getParentTables(childTableName)
{
	var parentTables = [];
	var table = new GlideRecord('sys_db_object');
	table.addQuery('name', childTableName);
	table.addNotNullQuery('super_class');
	table.query();
	if(table.next())
		{
		var parentTableName = table.super_class.name;
		parentTables.push(parentTableName);
		
		// get the parents of the parent and appen it
		parentTables.concat(getParentTables(parentTableName));
	}
	return parentTables;
}

 

Greetings

Fabian

I tried the above script, but I'm getting only one table, not the whole hierarchy

for 'cmdb_ci_computer'..I'm getting only 'cmdb_ci_hardware'

Jeah, i forgot one part. The below one should work:

function getParentTables(childTableName)
{
	var parentTables = [];
	var table = new GlideRecord('sys_db_object');
	table.addQuery('name', childTableName);
	table.addNotNullQuery('super_class');
	table.query();
	if(table.next())
		{
		var parentTableName = table.super_class.name;
		parentTables.push(parentTableName);
		
		// get the parents of the parent and appen it
		parentTables = parentTables.concat(getParentTables(parentTableName));
	}
	return parentTables;
}

Greetings

Fabian

chidanandadhath
Kilo Guru

I'm getting output like 

cmdb_ci_hardware,cmdb_ci,cmdb,

for 'cmdb_ci_computer'... I want it like

cmdb_ci_computer,cmdb_ci_hardware,cmdb_ci,cmdb,