Enviroment field update

PaulSylo
Tera Sage
Tera Sage

Hi All,

 

Can we update an Environment field for child class CI, as per parent class I? Ex: if my Server is parent class and it is in "Production" can  Child class like Memory , disk , disk partition can also be marked as "Production"

Regards,
PaulSylo

Kindly mark "helpful", if this helps, or Mark as "Accepted " if it solves your issues !
2 ACCEPTED SOLUTIONS

Community Alums
Not applicable

Hi @PaulSylo ,

 

Yes, this can be done using a after business rule, when the Environment field on the parent CI is updated, the same value is propagated to its related child CIs.
You can follow the below logic and update the scripts as per your instance.

(function executeRule(current, previous /*null when async*/) {
    if (current.environment != previous.environment) {
        var childCIs = new GlideRecord('child_class_table'); // Replace 'child_class_table' with the actual table of child CIs
        childCIs.addQuery('parent', current.sys_id);
        childCIs.query();
        while (childCIs.next()) {
            childCIs.environment = current.environment; // Update the Environment field
            childCIs.update();
        }
    }
})(current, previous);

 

Note: Update the fields, Table names as per your instance.

 

 

View solution in original post

You can make @Community Alums’s correct solution more efficient by using ‘updateMultiple(), which will do all the updates as a single database call vs looping and saving each record. Replace lines 5 -9 with the following.

childCIs.query();

childCIs.setValue('environment', currentEnvironment);

childCIs.updateMultiple();

 

View solution in original post

2 REPLIES 2

Community Alums
Not applicable

Hi @PaulSylo ,

 

Yes, this can be done using a after business rule, when the Environment field on the parent CI is updated, the same value is propagated to its related child CIs.
You can follow the below logic and update the scripts as per your instance.

(function executeRule(current, previous /*null when async*/) {
    if (current.environment != previous.environment) {
        var childCIs = new GlideRecord('child_class_table'); // Replace 'child_class_table' with the actual table of child CIs
        childCIs.addQuery('parent', current.sys_id);
        childCIs.query();
        while (childCIs.next()) {
            childCIs.environment = current.environment; // Update the Environment field
            childCIs.update();
        }
    }
})(current, previous);

 

Note: Update the fields, Table names as per your instance.

 

 

You can make @Community Alums’s correct solution more efficient by using ‘updateMultiple(), which will do all the updates as a single database call vs looping and saving each record. Replace lines 5 -9 with the following.

childCIs.query();

childCIs.setValue('environment', currentEnvironment);

childCIs.updateMultiple();