Join us Dec. 16 for The Community Hour, our first global virtual User Group event!

instanceOf in scoped application?

Peter Farr
Giga Contributor

I need to do the equivalent of gr.instanceOf("cmdb_ci_hardware") in a scoped application. The only thing I can come up with is something like this:, which does not seem very efficient. Is there a better way? I am trying to see if a CI is in the Hardware class or any of its subclasses (e.g. Computer).

Bonus question: why is instanceOf missing in the scoped API?

   _isHardware: function(ci){
    
        var related = new GlideRecord("cmdb_ci_hardware");
        related.addQuery("sys_id", ci.sys_id);
        related.addActiveQuery();
        related.setLimit(1);
        related.query();
        if ( related.next() ) {
            return true;
        }
        else {
           return false;
        }
        
    }
1 ACCEPTED SOLUTION

Check out Scoped GlideTableHierarchy

var table = new GlideTableHierarchy("cmdb_ci_computer"); 
var tables = table.getHierarchy();
var isHardware = (tables.indexOf("cmdb_ci_hardware") > 0);

View solution in original post

6 REPLIES 6

@paulmorris 

GlideTableHierarchy API is for scoped application. Do we have any API for global scope? OR Can we use INSTANCEOF operator to get all parent or child class names

Thanks for the reply Deepak, but I am not looking for the class of the object. I want to know if the object is a subclass of another class. For example, in a legacy application I can instantiate a GlideRecord of class Computer:

computerCI = new GlodeRecord("ci_cmdb_computer");

computerCI.addQuery"SOME NAME");

computerCI.query();

 

After this the following calls both return "true" because Computer is a subclass of Hardware:

computerCI.instanceOf("cmdb_ci_computer");

computerCI.instanceOf("cmdb_ci_hardware");