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

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

 

Thanks a lot this solves my issue.

--Sai Krishna

Rafał Rataj
Tera Contributor

Actually if you look on the query 

RafaRataj_1-1741169269741.png

you have your answer, do not use the class but use the class path 

 

var childClass = gr.child.sys_class_path.toString();
            if (childClass.startsWith("/!!/!2/!(/!!")){ // is Instance of server
                children.push(childSysId);
                }

RafaRataj_0-1741169239644.png