Impact field calculation using Risk condition

ServiceNow10sun
Giga Guru

Hi All, 

 

I am trying to calculate impact field in change request form  based on the two fields (Customized fields) both are choice fields each field has choices. I am trying to assign values to each choice when user select the choice and based on the total of the value the impact field should get populated. Below is my script which i have given in Risk condition, but its not working. 

 

(function executeRule(current, previous /*null when async*/) {
var aValue = current.u_impact_during_implementation.getDisplayValue();
var bValue = current.u_impact_of_failure.getDisplayValue();

// Map choices to their corresponding values
var valuesMap = {
'no_impact': 10,
'degradation': 60,
'loss_of_service': 60,
'service_outage': 95,
'minimal_failure': 10,
'moderate_failure': 70,
};

// Calculate total value
var totalValue = valuesMap[aValue] + valuesMap[bValue];

// Set impact field based on total value
if (totalValue <= 50) {
current.impact = 3; // Low impact
} else if (totalValue <= 80) {
current.impact = 2; // Moderate impact
} else {
current.impact = 1; // High impact
}
})(current, previous);

 

 

Please suggest.

ServiceNow10sun_0-1708537993840.png

ServiceNow10sun_0-1708538489054.png

 

 

 

 

(function executeRule(current, previous /*null when async*/) {
    var aValue = current.u_impact_during_implementation.getDisplayValue();
    var bValue = current.u_impact_of_failure.getDisplayValue();
   
    // Map choices to their corresponding values
    var valuesMap = {
        'no_impact': 10,
        'degradation': 60,
        'loss_of_service': 60,
        'service_outage': 95,
        'minimal_failure': 10,
        'moderate_failure': 70,
    };

    // Calculate total value
    var totalValue = valuesMap[aValue] + valuesMap[bValue];

    // Set impact field based on total value
    if (totalValue <= 50) {
        current.impact = 3; // Low impact
    } else if (totalValue <= 80) {
        current.impact = 2; // Moderate impact
    } else {
        current.impact = 1; // High impact
    }
})(current, previous);

 

 

@Amit Gujarathi  @Dr Atul G- LNG 

 
 
 
1 ACCEPTED SOLUTION

Thank you @Aman Kumar S ,

Yes i picked up incorrect API there , corrected it and its working fine now . Thank you once again .

Thank you @Dr Atul G- LNG 

==============================================================================

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

    // Add your code here
 
    var aValue = current.getValue("u_impact_during_implementation");
    var bValue = current.getValue("u_impact_of_failure");
gs.log("A Backend value :"+aValue);
gs.log(" B Backend Value :"+bValue);
   
    var valuesMap = {
        'no_impact': 10,
        'degradation': 60,
        'loss_of_service': 60,
        'service_outage': 95,
        'minimal_failure': 10,
        'moderate_failure': 70,
    };

   

    // Calculate total value
    var totalValue = valuesMap[aValue] + valuesMap[bValue];
   
    gs.log("getting total value sum"+totalValue);
gs.log("A value in number :"+valuesMap[aValue]);
gs.log("B value in number :"+valuesMap[bValue]);
    // Set impact field based on total valuex
    gs.log("Total of A and B value : "+totalValue);
    gs.log(" Testing 123");

    if (totalValue <= 50) {
        gs.log("testing 1");
        current.impact= 3; // Low impact

    }
   else if ((totalValue >50)&&(totalValue <=80))
    {
        gs.log("testing 2");
        current.impact = 2; // Moderate impact

    }
  else  if(totalValue>80)
  {

        gs.log("testing 3");

        current.impact = 1; // High impact
    }
    current.update();

})(current, previous);

 

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

    // Add your code here
  
    var aValue = current.getValue("u_impact_during_implementation");
    var bValue = current.getValue("u_impact_of_failure");
gs.log("A Backend value :"+aValue);
gs.log(" B Backend Value :"+bValue);
   
    var valuesMap = {
        'no_impact': 10,
        'degradation': 60,
        'loss_of_service': 60,
        'service_outage': 95,
        'minimal_failure': 10,
        'moderate_failure': 70,
    };

	

    // Calculate total value
    var totalValue = valuesMap[aValue] + valuesMap[bValue];
	
	gs.log("getting total value sum"+totalValue);
gs.log("A value in number :"+valuesMap[aValue]);
gs.log("B value in number :"+valuesMap[bValue]);
    // Set impact field based on total valuex
	gs.log("Total of A and B value : "+totalValue);
	gs.log(" Testing 123");

    if (totalValue <= 50) {
        gs.log("testing 1");
        current.impact= 3; // Low impact

    }
   else if ((totalValue >50)&&(totalValue <=80))
    {
        gs.log("testing 2");
        current.impact = 2; // Moderate impact

    }
  else  if(totalValue>80)
  {

        gs.log("testing 3");

        current.impact = 1; // High impact
    }
	current.update();

})(current, previous);​

 

View solution in original post

5 REPLIES 5

Ademir Amaral1
Kilo Sage

Hi @ServiceNow10sun 

Look, your script seems to be correct, I didn't find anything that wouldn't make it work.
I'm commenting here to follow up, in case anyone finds the solution.

If you just want to check if the fields are correct, and if the options correspond correctly too, that would be interesting.

Dr Atul G- LNG
Tera Patron
Tera Patron

Hi @ServiceNow10sun 

 

Sorry mate, scripting is my weak area. 

 

@Sohail Khilji @Aman Kumar S  any help.

*************************************************************************************************************
If my response proves useful, please indicate its helpfulness by selecting " Accept as Solution" and " Helpful." This action benefits both the community and me.

Regards
Dr. Atul G. - Learn N Grow Together
ServiceNow Techno - Functional Trainer
LinkedIn: https://www.linkedin.com/in/dratulgrover
YouTube: https://www.youtube.com/@LearnNGrowTogetherwithAtulG
Topmate: https://topmate.io/atul_grover_lng [ Connect for 1-1 Session]

****************************************************************************************************************

Aman Kumar S
Kilo Patron

Hi @ServiceNow10sun 

Are you sure you want display value of the choice field, since the keys that you have used in the "valuesMap" variable, it looks more backend value for the choices to me.

Can you confirm on this, instead of using getDIsplayValue, try using

var aValue = current.getValue("u_impact_during_implementation");
var bValue = current.getValue("u_impact_of_failure");

Best Regards
Aman Kumar

Thank you @Aman Kumar S ,

Yes i picked up incorrect API there , corrected it and its working fine now . Thank you once again .

Thank you @Dr Atul G- LNG 

==============================================================================

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

    // Add your code here
 
    var aValue = current.getValue("u_impact_during_implementation");
    var bValue = current.getValue("u_impact_of_failure");
gs.log("A Backend value :"+aValue);
gs.log(" B Backend Value :"+bValue);
   
    var valuesMap = {
        'no_impact': 10,
        'degradation': 60,
        'loss_of_service': 60,
        'service_outage': 95,
        'minimal_failure': 10,
        'moderate_failure': 70,
    };

   

    // Calculate total value
    var totalValue = valuesMap[aValue] + valuesMap[bValue];
   
    gs.log("getting total value sum"+totalValue);
gs.log("A value in number :"+valuesMap[aValue]);
gs.log("B value in number :"+valuesMap[bValue]);
    // Set impact field based on total valuex
    gs.log("Total of A and B value : "+totalValue);
    gs.log(" Testing 123");

    if (totalValue <= 50) {
        gs.log("testing 1");
        current.impact= 3; // Low impact

    }
   else if ((totalValue >50)&&(totalValue <=80))
    {
        gs.log("testing 2");
        current.impact = 2; // Moderate impact

    }
  else  if(totalValue>80)
  {

        gs.log("testing 3");

        current.impact = 1; // High impact
    }
    current.update();

})(current, previous);

 

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

    // Add your code here
  
    var aValue = current.getValue("u_impact_during_implementation");
    var bValue = current.getValue("u_impact_of_failure");
gs.log("A Backend value :"+aValue);
gs.log(" B Backend Value :"+bValue);
   
    var valuesMap = {
        'no_impact': 10,
        'degradation': 60,
        'loss_of_service': 60,
        'service_outage': 95,
        'minimal_failure': 10,
        'moderate_failure': 70,
    };

	

    // Calculate total value
    var totalValue = valuesMap[aValue] + valuesMap[bValue];
	
	gs.log("getting total value sum"+totalValue);
gs.log("A value in number :"+valuesMap[aValue]);
gs.log("B value in number :"+valuesMap[bValue]);
    // Set impact field based on total valuex
	gs.log("Total of A and B value : "+totalValue);
	gs.log(" Testing 123");

    if (totalValue <= 50) {
        gs.log("testing 1");
        current.impact= 3; // Low impact

    }
   else if ((totalValue >50)&&(totalValue <=80))
    {
        gs.log("testing 2");
        current.impact = 2; // Moderate impact

    }
  else  if(totalValue>80)
  {

        gs.log("testing 3");

        current.impact = 1; // High impact
    }
	current.update();

})(current, previous);​