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.

Calculated value for CMDB for rack units occupied.

Piemur1
Tera Contributor

Hello, I wanted to set up an automatic calculation for how many rack units are occupying a rack. I've created a field entry for the devices with the expectation that they will have a value for how many rack units they will occupy (such as servers and any other devices that could be installed within a rack such as switches, routers, PSUs, etc). And then the plan would be for using the CI Relationship "contains::contained by" to pull the items within the rack and then sum up the rack units and display for the "Rack units in use" field on a given Rack. Currently I have the field on the Servers table (cmdb_ci_servers) and planning on adding the field to more later as we identify what items will be tracked in the Racks. I presume I will be using the Script type rather than Formula.

1 ACCEPTED SOLUTION

HIROSHI SATOH
Mega Sage

Instead of creating a new GlideRecord, you can directly access the child CI like this:

// Initialize the sum
var totalRackUnits = 0;

// Get the list of CIs contained by the rack
var grContainment = new GlideRecord('cmdb_rel_ci');
grContainment.addQuery('parent', current.sys_id);  // Assuming current is the Rack CI
grContainment.addQuery('type.name', 'contains::contained by');
grContainment.query();

while (grContainment.next()) {
    if (grContainment.child) {  // Directly check if the child CI exists
        totalRackUnits += parseInt(grContainment.child.u_rack_units || 0);  // Access the rack units field from child CI
    }
}

// Update the rack's "Rack units in use" field
current.u_rack_units_in_use = totalRackUnits;

View solution in original post

5 REPLIES 5

HIROSHI SATOH
Mega Sage

Instead of creating a new GlideRecord, you can directly access the child CI like this:

// Initialize the sum
var totalRackUnits = 0;

// Get the list of CIs contained by the rack
var grContainment = new GlideRecord('cmdb_rel_ci');
grContainment.addQuery('parent', current.sys_id);  // Assuming current is the Rack CI
grContainment.addQuery('type.name', 'contains::contained by');
grContainment.query();

while (grContainment.next()) {
    if (grContainment.child) {  // Directly check if the child CI exists
        totalRackUnits += parseInt(grContainment.child.u_rack_units || 0);  // Access the rack units field from child CI
    }
}

// Update the rack's "Rack units in use" field
current.u_rack_units_in_use = totalRackUnits;