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.

Field calculation bases on business rule

New Developer_S
Giga Sage

Hi, I have 3 Fields 

Confidentiality , Integrity and Availability . All these have choices (1,2,3,4). I need to make a arithematic multiplication of these values and the results should be stored in business criticality field. As write the below before insert / update business rule, my code is not working. Can somebody tell me where i am went wrong ?

 

 

 

(function executeRule(current, previous /*null when async*/) {

var gr = new GlideRecord('cmdb_ci_service'); //Get the current record

var conf  = parseInt(gr.getValue('u_confidentiality')); // Get the value of field confidentiality
var intg = parseInt(gr.getValue(' u_integrity')); // Get the value of field Integrity
var avail = parseInt(gr.getValue('u_availability')); // Get the value of field availabilty
var result = parseInt( (conf * intg * avail) );

if (result == (1 || 2 || 3 || 4))
{
    current.busines_criticality = 1; // set the value of business criticality to critical 
    gs.info("Business Criticality is changed successfully to"  + current.busines_criticality.getDisplayValue()) ;

}

else if (result == (6 || 8 || 9 || 12))
{
current.busines_criticality = 2;// set the value of business criticality to high
gs.info("Business Criticality is changed successfully to"  + current.busines_criticality.getDisplayValue()) ;

}
else if (result == (16 || 18 || 24 || 27))
{
current.busines_criticality = 3; // set the value of business criticality to medium
gs.info("Business Criticality is changed successfully to"  + current.busines_criticality.getDisplayValue()) ;
}
else (result == (32 || 36 || 48 || 64))
{
current.busines_criticality = 4; // set the value of business criticality to low

gs.info("Business Criticality is changed successfully to"  + current.busines_criticality.getDisplayValue()) ;

}


})(current, previous);
4 REPLIES 4

Bert_c1
Kilo Patron

Try adding:

 

    gs.info("Business Criticality result is "  + result) ;

 

just before your "if" statement to see the value. Also, do any of the other gs.info(); results appear in Script Log Statements?

 

Maybe change the "if" conditions to:

 

if ((result == 1) || (result ==2) || (result == 3) || (result==4)) {

Kieran Anson
Kilo Patron

Have you considered using a data lookup definition rather than a business rule?

(function executeRule(current, previous /*null when async*/) {


var conf = Number(current.getValue('u_confidentiality'));
var integr = Number(current.getValue('u_integrity'));
var avail = Number(current.getValue('u_availability'));
var result  = Number( (conf * integr * avail) );

if(result <= 4){
	current.setValue('busines_criticality', '1') //Do check whether this should be business_criticality
	return;
}

if(result <= 12){
	current.setValue('busines_criticality', '2')
	return;
}

if(result <= 27){
	current.setValue('busines_criticality', '3')
	return;
}

if(result <= 64){
	current.setValue('busines_criticality', '4')
	return;
}
})(current, previous)

Community Alums
Not applicable

Hi @New Developer_S ,

Please check below script 

(function executeRule(current, previous /*null when async*/) {

var gr = new GlideRecord('cmdb_ci_service'); //Get the current record

var conf  = parseInt(gr.getValue('u_confidentiality')); // Get the value of field confidentiality
var intg = parseInt(gr.getValue(' u_integrity')); // Get the value of field Integrity
var avail = parseInt(gr.getValue('u_availability')); // Get the value of field availabilty
var result = parseInt( (conf * intg * avail) );

if (result == 1 || result == 2 || result == 3 || result == 4))
{
    current.busines_criticality = 1; // set the value of business criticality to critical 
    gs.info("Business Criticality is changed successfully to"  + current.busines_criticality.getDisplayValue()) ;

}

else if (result == 6 || result == 8 || result == 9 || result == 12)
{
current.busines_criticality = 2;// set the value of business criticality to high
gs.info("Business Criticality is changed successfully to"  + current.busines_criticality.getDisplayValue()) ;

}
else if (result == 16 || result == 18 || result == 24 || result == 27)
{
current.busines_criticality = 3; // set the value of business criticality to medium
gs.info("Business Criticality is changed successfully to"  + current.busines_criticality.getDisplayValue()) ;
}
else (result == 32 || result == 36 || result == 48 || result == 64)
{
current.busines_criticality = 4; // set the value of business criticality to low
gs.info("Business Criticality is changed successfully to"  + current.busines_criticality.getDisplayValue()) ;
}

 

Please mark my answer correct and helpful if this works for you

Thanks and Regards

Sarthak

New Developer_S
Giga Sage

@Bert_c1 @Kieran Anson @Community Alums 

 

Thank you all for wonderful support šŸ™‚ its working fine after setting up some logs and readjusting the code.