I want to set a custom field value "Risk Score" based on the user selected values in 2 single select custom fields, "Impact" and "Probability".
I've tried two different ways to address this based on other articles, but my approaches are not working. When I'm in the create issue screen, after I've set the value for both Impact and Probability, the risk score does not update. Any advice??
Approach 1 (based on Adaptavist Article on Set Priority Using an Impact-Urgent matrix)
def riskMatrix = [
"Critical (20-25)" : [
"5 - Highly Likely": "5 - High",
"5 - High": "5 - Highly Likely",
"5 - Highly Likely": "4 - Moderate",
"4 - Likely": "5 - High"
],
"Mid (10-19)" : [
"5 - Highly Likely": "3 - Low",
"5 - Highly Likely": "2 - Annoyance",
"4 - Likely": "3 - Low",
"4 - Likely": "4 - Moderate",
"3 - Possible": "5 - High",
"3 - Possible": "4 - Moderate",
"2 - Unlikely": "5 - High"
],
"Low (1-9)" : [
"5 - Highly Likely": "1 - No Effect",
"4 - Likely": "1 - No Effect",
"3 - Possible": "1 - No Effect",
"2 - Unlikely": "1 - No Effect",
"1 - Highly Unlikely": "1 - No Effect",
"4 - Likely": "2 - Annoyance",
"3 - Possible": "2 - Annoyance",
"2 - Unlikely": "2 - Annoyance",
"1 - Highly Unlikely": "2 - Annoyance",
"3 - Possible": "3 - Low",
"2 - Unlikely": "3 - Low",
"1 - Highly Unlikely": "3 - Low",
"2 - Unlikely": "4 - Moderate",
"1 - Highly Unlikely": "4 - Moderate",
"1 - Highly Unlikely": "5 - High",
]
]
def riskScoreField = getFieldById ("customfield_15704") //("Risk Score") //customfield_15704
def impactFieldValue = getFieldByName("Impact (Freestyle)").value //customfield_15709
def probabilityFieldValue = getFieldByName("Probability").value //customfield_15708
String riskScore = riskMatrix[probabilityFieldValue][impactFieldValue]
riskScoreField.setFormValue(riskScore)
Approach 2: based on https://community.atlassian.com/t5/Jira-questions/Behaviour-Plugin-Setting-Priority-based-on-value-in-a-Scripted/qaq-p/180062
def fimpact = getFieldByName("Impact (Freestyle)")
def fprobability = getFieldByName("Probability")
def friskscore = getFieldById("customfield_15704") //Risk Score
String Impact = (String) fimpact.getFormValue()
String Probability = (String) fprobability.getFormValue()
if (Impact == "5 - High" && Probability == "5 - Highly Likely") {
friskscore.setFormValue("Critical (20-25)")
} else if (Impact == "5 - High" && Probability == "4 - Likely") {
friskscore.setFormValue("Critical (20-25)")
} else if (Impact == "4 - Moderate" && Probability == "5 - Highly Likely") {
friskscore.setFormValue("Critical (20-25)")
} else if (Impact == "5 - High" && Probability == "2 - Unlikely") {
friskscore.setFormValue("Mid (10-19)")
} else if (Impact == "5 - High" && Probability == "1 - Highly Unlikely") {
friskscore.setFormValue("Low (1-9)")
Behaviours Config
In both cases, I've added a Server Side script to the Risk Score field.
Put your code in both the impact and probability fields.
The risk score sever-side script only runs when risk score is modified.
To change the risk score when the impact is modified, put the code in the impact server-side script ... and to change it when the probability field changes, put the code on that fields's server side script.
Or better, put your script on the server in a "calculateRiskScore()" method... then call that scrip and method in each of the 2 fields so that you only have to edit it in one place.
If you choose the script option, be sure to add at the top:
import com.onresolve.jira.groovy.user.FieldBehaviours
@BaseScript FieldBehaviours fieldBehaviours
def calculateRiskScore(){
/* your code here */
}
Thanks!
I ended up altering my approach to not allow the user to set the Risk Score at all and created a scripted field, which addressed the underlying need.
Below is the functional code.
///Script is used to automatically set the Risk Score based on Impact and Probability. The Critical and Mid level risk rules are defined. All others would be classifed as Low.
import com.atlassian.jira.component.ComponentAccessor
///get the values of Impact and Probability
def impact = getCustomFieldValue("Impact (Freestyle)").value //customfield_15709
def probability = getCustomFieldValue("Probability").value //customfield_15708
// Set Risk Score = Critical (20-25)
if (
( impact== "5 - High" && probability=="5 - Highly Likely") ||
( impact== "4 - Moderate" && probability=="5 - Highly Likely") ||
( impact== "5 - High" && probability=="4 - Likely")
)
{ return "Critical (20-25)" }
// Set Risk Score = Mid (10-19)
else if (
( impact== "3 - Low" && probability=="5 - Highly Likely") ||
( impact== "2 - Annoyance" && probability=="5 - Highly Likely") ||
( impact== "3 - Low" && probability=="4 - Likely") ||
( impact== "4 - Moderate" && probability=="4 - Likely") ||
( impact== "5 - High" && probability=="3 - Possible") ||
( impact== "4 - Moderate" && probability=="3 - Possible") ||
( impact== "5 - High" && probability=="2 - Unlikely")
)
{ return "Mid (10-19)" }
// Set Risk Score = Low (1-9)
else
return "Low (1-9)"
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.