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.

How to set highest business criticality filed value in script

jack33
Tera Expert

Hi All,

 

I am trying to get highest business criticality choice value to set on cmdb hadware,based on relation ship.

If any CI is having relationship with multiple application i want to set up Highest value of business criticality to hardware CI.

I tried below code but in array all values are getting stored

 

for example 2 CIs are there

CI A linked with application which have  business criticality > mission critical,business critical i want to set mission critical

if CI B linked with application which have  business criticality > non critical,core i want to set core

but in buss_criticality Array i am getting all values of business criticality from all the CI

var buss_criticality = [];
var ci_svc = new GlideRecord('svc_ci_assoc');
ci_svc.addEncodedQuery('service_id.sys_class_nameINSTANCEOFcmdb_ci_service_discovered^ci_id.sys_class_nameINSTANCEOFcmdb_ci_hardware^service_id.environmentINProduction,DR^ci_id.nameSTARTSWITHltin441268^ORci_id.nameSTARTSWITHCTSINCHNAZRF5DNS');
ci_svc.query();
while(ci_svc.next()){
buss_criticality.push(ci_svc.service_id.busines_criticality.toString());
var uniqueArray = new ArrayUtil().unique(buss_criticality);
var arrayUtil = new global.ArrayUtil();
var tst = arrayUtil.contains(uniqueArray,'Core');
var ci_hw = new GlideRecord('cmdb_ci_hardware');
ci_hw.addQuery('sys_id',ci_svc.ci_id);
ci_hw.query();
while(ci_hw.next()){
	if(tst){
ci_hw.setWorkflow(false);
ci_hw.u_business_criticality = '4';
ci_hw.update();
}
else if((uniqueArray.indexOf('Core')==-1) && (arrayUtil.contains(uniqueArray,'Mission Critical'))){
ci_hw.setWorkflow(false);
ci_hw.u_business_criticality = '0';
ci_hw.update();
}
else if((uniqueArray.indexOf('Core')==-1) && (uniqueArray.indexOf('Mission Critical')==-1) && (arrayUtil.contains(uniqueArray,'Business Critical'))){
ci_hw.setWorkflow(false);
ci_hw.u_business_criticality = '1';
ci_hw.update();
}
else if((uniqueArray.indexOf('Core')==-1) && (uniqueArray.indexOf('Mission Critical')==-1) && (uniqueArray.indexOf('Business Critical')==-1) && (arrayUtil.contains(uniqueArray,'Non Critical'))){
ci_hw.setWorkflow(false);
ci_hw.u_business_criticality = '2';
ci_hw.update();
}
else if((uniqueArray.indexOf('Core')==-1) && (uniqueArray.indexOf('Mission Critical')==-1) && (uniqueArray.indexOf('Business Critical')==-1) && (uniqueArray.indexOf('Non Critical')==-1) && (arrayUtil.contains(uniqueArray,'Undefined'))){
ci_hw.setWorkflow(false);
ci_hw.u_business_criticality = '3';
ci_hw.update();
}
}}

Can you please, thanks in advance.

 

1 ACCEPTED SOLUTION

Mark Manders
Mega Patron

You are processing all criticality at once. I think iterating through them could solve it, but you should create some mapping:

 

var criticalityRank = {
    "Mission Critical": 0,
    "Business Critical": 1,
    "Core": 2,
    "Non Critical": 3,
    "Undefined": 4
};

var ci_svc = new GlideRecord('svc_ci_assoc');
ci_svc.addEncodedQuery('service_id.sys_class_nameINSTANCEOFcmdb_ci_service_discovered^ci_id.sys_class_nameINSTANCEOFcmdb_ci_hardware^service_id.environmentINProduction,DR^ci_id.nameSTARTSWITHltin441268^ORci_id.nameSTARTSWITHCTSINCHNAZRF5DNS');
ci_svc.query();

while (ci_svc.next()) {
    var ci_hw = new GlideRecord('cmdb_ci_hardware');
    ci_hw.get(ci_svc.ci_id);

    var highestCriticality = 4; // Start with "Undefined" which is the lowest

    var svc_criticality = new GlideRecord('svc_ci_assoc');
    svc_criticality.addQuery('ci_id', ci_svc.ci_id);
    svc_criticality.query();

    while (svc_criticality.next()) {
        var currentCriticality = svc_criticality.service_id.busines_criticality.toString();
        if (criticalityRank[currentCriticality] < highestCriticality) {
            highestCriticality = criticalityRank[currentCriticality];
        }
    }
    ci_hw.setWorkflow(false); // Disable workflow if needed
    ci_hw.u_business_criticality = highestCriticality.toString();
    ci_hw.update();
}

Please mark any helpful or correct solutions as such. That helps others find their solutions.
Mark

View solution in original post

5 REPLIES 5

I updated values like below which is same as backend value in system:

"Core": -0.5,
    "Mission Critical": 0,
    "Business Critical": 1,
    "Non Critical": 2,
    "Undefined": 3
 
And now it is working.
Thank you so much mark i was struggling alot. With your help issue got resolved quickly
 
Thank you