The CreatorCon Call for Content is officially open! Get started here.

What is the best way to update the Class of a CI when the Model ID changes?

Marcel H_
Tera Guru

I have a requirement to automatically update the Class (table) of a CI if the Model ID changes. While the Configuration Item table has Class as a System Class Name type, Model has CMDB CI Class but that is a string, and Model Categories has CI Class which is a Table Name type.

 

None of these types are the same so there doesn't seem to be an easy way to handle this via a simple Business Rule, and I'd really like to avoid scripting every possible scenario to have it update automatically. Has anyone run into a similar situation that they've created a solution for? 

1 ACCEPTED SOLUTION

I ended up creating before Business Rule that triggers when Model ID changes and runs the following script. Seems to work consistently. 

(function executeRule(current, previous) {
    
	var ciClass = current.getValue("sys_class_name"); // Get current sys_class_name

    // Get the Model Category CI Class
    var modCatClass = "";
    if (current.model_id) {
        var modelGR = new GlideRecord("cmdb_model_category");
        if (modelGR.get(current.model_id.u_primary_category)) {
            modCatClass = modelGR.getValue("cmdb_ci_class");
        }
    }

    // Updating sys_class_name requires GlideRecord on the same table
    if (modCatClass && ciClass !== modCatClass) {
        var updateGR = new GlideRecord(current.getTableName());
        if (updateGR.get(current.sys_id)) {
            updateGR.setValue("sys_class_name", modCatClass);
            updateGR.update(); // Save the change
        }
    }

})(current, previous);

View solution in original post

3 REPLIES 3

Runjay Patel
Giga Sage

Hi @Marcel H_ ,

you can write br on model I’d change and set the files value in action section , no need of writing scripts.

The problem is that since the fields on each of the 3 tables; CI, Model and Model Category, are different field types you can't even select any of the fields to set to Same As.

I ended up creating before Business Rule that triggers when Model ID changes and runs the following script. Seems to work consistently. 

(function executeRule(current, previous) {
    
	var ciClass = current.getValue("sys_class_name"); // Get current sys_class_name

    // Get the Model Category CI Class
    var modCatClass = "";
    if (current.model_id) {
        var modelGR = new GlideRecord("cmdb_model_category");
        if (modelGR.get(current.model_id.u_primary_category)) {
            modCatClass = modelGR.getValue("cmdb_ci_class");
        }
    }

    // Updating sys_class_name requires GlideRecord on the same table
    if (modCatClass && ciClass !== modCatClass) {
        var updateGR = new GlideRecord(current.getTableName());
        if (updateGR.get(current.sys_id)) {
            updateGR.setValue("sys_class_name", modCatClass);
            updateGR.update(); // Save the change
        }
    }

})(current, previous);