Help in GlideRecord Instance Of

sai krishna10
Giga Guru

Hi,

 

By using the below script 

var table = new GlideRecord('cmdb_ci');
table.addEncodedQuery('sys_class_nameINSTANCEOFcmdb_ci_server');
table.query();
while(table.next()){
gs.log(table.sys_class_name);
}

Then the output is giving 

cmdb_ci_aix_server,cmdb_ci_unix_server,cmdb_ci_win_server etc.

 

I want a script so that if i given cmdb_ci_aix_server, i want a output that this class is a Instanceof cmdb_ci_server class.

Is this possible

 

 

Thanks

Sai Krishna 

1 ACCEPTED SOLUTION

The issue is that cmdb_ci_aix_server has multiple levels of hierarchy. You'll need some intelligent way to determine at exactly which level you want to stop. Yes, cmdb_ci_aix_server extends cmdb_ci_unix_server, which in turn extends cmdb_ci_server, but then cmdb_ci_server extends cmdb_ci_computer, which extends cmdb_ci_hardware, which extends cmdb_ci, which extends cmdb.

The question is, how do you know that you want cmdb_ci_server, versus any of the other tables in the chain? Do you have an array of "base" tables that, when you reach one of those, you consider the hierarchy resolved? If so, this code demonstrates how to resolve whether your table extends one of your designated base tables:

(function() {
    var extendedTable = 'cmdb_ci_aix_server';
    var baseTables = [
        'cmdb_ci_ups',
        'cmdb_ci_printer',
        'cmdb_ci_server',
        'cmdb_ci_database',
        'cmdb_ci_business_app',
        // ...and so on
    ];
    
    var aUtil = new ArrayUtil();
    var tUtil = new TableUtils(extendedTable);
    var tables = tUtil.getTables();
    var msg = '';
    for (var index = 0; index < tables.size(); index++) {
        if (aUtil.contains(baseTables, tables.get(index))) {
            msg = 'Table ' + extendedTable + ' is extended from ' +
                    tables.get(index) + '.';
            break;
        }
    }
    if (!msg)
        msg = 'Table ' + extendedTable + ' not extended from any ' +
                'table in the base table hierarchy.';
    
    gs.info(msg);
})();

If, on the other hand, you just want to see if your table extends from cmdb_ci_server at any level, that's easy enough to do:

(function() {
    var extendedTable = 'cmdb_ci_aix_server';
    var baseTable = 'cmdb_ci_server';
    
    var tUtil = new TableUtils(extendedTable);
    var tables = tUtil.getTables();
    
    var msg = 'Table ' + extendedTable + ' does ';
    if (tables.indexOf(baseTable) < 0)
        msg += 'not ';
    msg += 'extend from ' + baseTable + '.';
    gs.info(msg);
})();

Does that help get in the neighborhood of what you're looking for? If not, how exactly are you determining which table in the hierarchy you're looking for?

Hope this helps,
--Dennis R

 

View solution in original post

12 REPLIES 12

yes i tried all those , but unable to find the solution.

if i use the solution provided by sanjiv it is giving output as cmdb_ci_unix_Server , because the table cmdb_ci_aix_server is extended from cmdb_ci_unix_server

cmdb_ci_unix_Server extended from cmdb_ci_server means it is two level hierarchy but if i use cmdb_ci_win_server then ouout is giving as cmdb_ci_server which is one level hierarchy,

But by using the Instanceof cmdb_ci_server it is giving cmdb_ci_aix_server and cmdb_ci_win_server. I want the output as cmdb_ci_server even the input is cmdb_ci_aix_server or cmdb_ci_win_server. Can you pls guide me to achieve this

 

Thanks

Sai Krishna

Try this.

 

 

gs.log(getParent('cmdb_ci_aix_server'));

 

function getParent(tablename)

{

var db = new GlideRecord('sys_db_object');

db.addQuery('sys_class_name',tablename);

db.query();

if (db.next()){

if (db.super_class.sys_class_name!='cmdb_ci')

getParent(db.super_class.sys_class_name);

else

return db.sys_class_name;

}

}


Please mark this response as correct or helpful if it assisted you with your question.

No it is not working.

Ok. This one should work

 

var test = this.getParent('cmdb_ci_aix_server');
gs.info(test);


function getParent(tablename)
{
var db = new GlideRecord('sys_db_object');
db.addQuery('name',tablename);
db.query();
var b ='';

if (db.next()){

if (db.super_class.label=='Server')
{
var a = db.super_class.name;
return a.toString();
}
else
{
b = this.getParent(db.super_class.name);
}

}
return b;

}

Please mark this response as correct or helpful if it assisted you with your question.

Thanq Sanjiv for your responses. 🙂