Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

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);

ServiceNow Nerd
ServiceNow Developer MVP 2020-2022
ServiceNow Community MVP 2019-2022

View solution in original post

6 REPLIES 6

Deepak Ingale1
Mega Sage

Hi,

You can use getRecordClassName() method to return the class of the CI.

 

https://developer.servicenow.com/app.do#!/api_doc?v=kingston&id=r_ScopedGlideRecordGetRecordClassNam...

 

Now, why it is not working in scoped app, only ServiceNow can answer it. But you have the above alternative what you can go with.

 

Note: Please mark reply as correct / helpful if it has answered your question.

Thanks for the response Deepak, but I am not looking for the class. I am trying to find out whether the CI is a subclass of another. For example in a legacy application, taking a CI of class Computer, instanceOf("cmdb_ci_hardware") will return true because Computer is a subclass of Hardware.

Check out Scoped GlideTableHierarchy

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

ServiceNow Nerd
ServiceNow Developer MVP 2020-2022
ServiceNow Community MVP 2019-2022

Perfect Paul. I missed that one. Thanks.