Query on Script

Joshuu
Kilo Sage

Hi All,

 

Below is the Change Risk Assessment Script. And it is working perfectly fine. Here, I am checking the Impacted Services/CIs from the related lists and checking the relationships. 

 

So, the extended requirement is to include the affected CIs as well in this script. Once we get the count of both(Impacted and Affected CIs) we need to take the sum of both.

 

var cr = new GlideRecord('task_cmdb_ci_service');
cr.addQuery('task', primary);
cr.query();
while (cr.next()) {
    var ci = new GlideAggregate('cmdb_rel_ci');
    ci.addQuery('parent', cr.cmdb_ci_service.sys_id);
    ci.addEncodedQuery('child.busines_criticality=1');
    ci.addAggregate('COUNT');
    ci.query();
    while (ci.next()) {
        actual_result = ci.getAggregate('COUNT');
        if (actual_result >= 1) {
            scaled_result = 20;
        } else {
            scaled_result = 10;
        }
    }
}

 

 

I have tried the below script but it is not working when there is no impacted ci in the change. I think I am missing some logic.

 

var cr1 = new GlideRecord('task_ci');
cr1.addQuery('task', primary);
cr1.query();
gs.info('getChange :' + primary);
while (cr1.next()) {
    var ci1 = new GlideAggregate('cmdb_rel_ci');
    ci1.addQuery('parent', cr1.ci_item.sys_id);
    ci1.addEncodedQuery('child.ref_cmdb_ci_computer.u_business_criticality=1');
    ci1.addAggregate('COUNT');
    ci1.query();
    while (ci1.next()) {
        var getAff = ci1.getAggregate('COUNT');
        gs.info('Affected! getAff=' + getAff);
    }


    var cr = new GlideRecord('task_cmdb_ci_service');
    cr.addQuery('task', primary);
    cr.query();
    gs.info('getChange :' + primary);
    while (cr.next()) {
        var ci = new GlideAggregate('cmdb_rel_ci');
        ci.addQuery('parent', cr.cmdb_ci_service.sys_id);
        ci.addEncodedQuery('child.ref_cmdb_ci_computer.u_business_criticality=1');
        ci.addAggregate('COUNT');
        ci.query();
        while (ci.next()) {
            var getImp = ci.getAggregate('COUNT');
            gs.info('Impacted! getImp=' + getImp);
        }

        actual_result = getAff + getImp;
        gs.info('actual_result=' + actual_result);

        if (actual_result >= 1) {
            scaled_result = 20;
        } else {
            scaled_result = 10;
        }
    }
}

 

Could you please assist.

 

Thanks & Regards.

2 REPLIES 2

Maddysunil
Kilo Sage

@Joshuu 

It looks like the issue in your second script might be related to the scope of your variables getAff and getImp. They are defined inside the while loops, so if there is no affected CI or impacted CI, the variables won't be defined, leading to potential errors when you try to sum them up.

 

Please Accept the solution if it helps you

@Joshuu 

Ypu can change your code like this

var getAff = 0; // Initialize variables outside the loops
var getImp = 0;

var cr1 = new GlideRecord('task_ci');
cr1.addQuery('task', primary);
cr1.query();
gs.info('getChange :' + primary);

while (cr1.next()) {
    var ci1 = new GlideAggregate('cmdb_rel_ci');
    ci1.addQuery('parent', cr1.ci_item.sys_id);
    ci1.addEncodedQuery('child.ref_cmdb_ci_computer.u_business_criticality=1');
    ci1.addAggregate('COUNT');
    ci1.query();

    while (ci1.next()) {
        getAff = ci1.getAggregate('COUNT');
        gs.info('Affected! getAff=' + getAff);
    }
}

var cr = new GlideRecord('task_cmdb_ci_service');
cr.addQuery('task', primary);
cr.query();
gs.info('getChange :' + primary);

while (cr.next()) {
    var ci = new GlideAggregate('cmdb_rel_ci');
    ci.addQuery('parent', cr.cmdb_ci_service.sys_id);
    ci.addEncodedQuery('child.ref_cmdb_ci_computer.u_business_criticality=1');
    ci.addAggregate('COUNT');
    ci.query();

    while (ci.next()) {
        getImp = ci.getAggregate('COUNT');
        gs.info('Impacted! getImp=' + getImp);
    }
}

actual_result = getAff + getImp;
gs.info('actual_result=' + actual_result);

if (actual_result >= 1) {
    scaled_result = 20;
} else {
    scaled_result = 10;
}