- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-21-2024 10:02 AM - edited 02-21-2024 10:05 AM
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.
(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
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-23-2024 12:02 AM
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);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-21-2024 10:35 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-22-2024 03:40 AM
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]
****************************************************************************************************************

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-22-2024 06:48 PM
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");
Aman Kumar
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-23-2024 12:02 AM
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);