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

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

Hi Mark, Thank you for your help.

 

I can try this script but in my system backend values for choices are like below:

 "Mission Critical": 0,
    "Business Critical": 1,
    "Core": 4,
    "Non Critical": 2,
    "Undefined": 3
And descending order is>
Core> mission critical >business critical> non critical > undefined. And i cant change these value.
Can you please suggest any other option/ modification in your script?
Thank you

The values of the exact choices don't matter. The mapping done at the beginning of the script has everything to do with mapping them to do the calculation in the script. So 'Mission Critical' could have a (technical) value of 100, it will still be the lowest for this script, because it's the most important one.  Mapping is done to see which one is the highest. And if at any time you get a sixth, or only 4, you can just change the mapping so you will always the lowest number from the script, getting to the highest criticality.


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

Understood Mark,

 

I tried code with value:

     "Core": 0,
    "Mission Critical": 1,
    "Business Critical": 2,
    "Non Critical": 3,
    "Undefined": 4
 
For CI A i was expecting value to be set as CORE ,but script updated it as mission critical AND
for CI B  i was expecting value to be set as Business Critical ,but script updated it as Non Critical.
 
In variable> highestCriticality ,i am getting value as 0 and 2 but CI updated with the next value 
Can you please check once