Querying a CMDB sub table

Mark Van Loock
Tera Guru

Help! There must be an easier way than this...

I need to query the CI field on the change request form to check if the CI is flagged as business critical. I thought this would be so easy, but the "business_criticality" field isn't in the parent CMDB_CI table - it exists in a number of extended tables.

 

I was hoping I could simply dotwalk to the value I wanted, but I don't seem to be able to do it - I just get "undefined" returned.

 

The below script works - it get the CI, queries the CMDB to find which table the CI is in, then does a second query on that table to get the value. It hardly seems elegant. Is there a really simple way that I'm missing?

 

Thank you!

 

// Get the CI listed on the change form. For this script, I'm forcing one in.
var sys_id = "4a2c825c1b379650c7516351f54bcb61";

//Run a query on the CMDB table as I need to get the class name
var ci = new GlideRecord('cmdb_ci');
ci.addQuery('sys_id',sys_id);
ci.query();
if (ci.next()) {
gs.print(ci.name); //I've got the correct CI
gs.print(ci.sys_class_name); //I've got the correct Class name

gs.print(ci.ref_cmdb_ci_business_app.business_criticality); //This command gives me the business criticality value I'm after, but the CI may not always be in the business_app table.

//Run a query on the table that I know the CI sits in.
var cl = new GlideRecord(ci.sys_class_name)
cl.addQuery('sys_id',sys_id);
cl.query();
if (cl.next()) {
gs.print(cl.business_criticality); // Got the value I want.
}
}

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

@Mark Van Loock 

try this

var sys_id = "4a2c825c1b379650c7516351f54bcb61"; // Your CI sys_id
var ciGr = new GlideRecord('cmdb_ci');

if (ciGr.get(sys_id)) {
    // Single-line dot-walk using ref_cmdb_ci
    var criticality = ciGr.ref_cmdb_ci.business_criticality.getDisplayValue();
    
    if (criticality) {
        gs.print("Business Criticality: " + criticality);
    } else {
        gs.print("No criticality found for this CI type");
    }
}

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

View solution in original post

3 REPLIES 3

Ankur Bawiskar
Tera Patron
Tera Patron

@Mark Van Loock 

try this

var sys_id = "4a2c825c1b379650c7516351f54bcb61"; // Your CI sys_id
var ciGr = new GlideRecord('cmdb_ci');

if (ciGr.get(sys_id)) {
    // Single-line dot-walk using ref_cmdb_ci
    var criticality = ciGr.ref_cmdb_ci.business_criticality.getDisplayValue();
    
    if (criticality) {
        gs.print("Business Criticality: " + criticality);
    } else {
        gs.print("No criticality found for this CI type");
    }
}

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

Wow! Thank you. I must have tried every combination except that one!! That's exactly what I was after. Much appreciated.

Kind Regards,

Mark.

@Mark Van Loock 

Glad to help

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader