Behaviour Plugin: Setting Priority based on value in a Scripted Field

I created a scripted field called Severity which returns a value from 1-5 based on Impact and Urgency. I now want to update the JIRA default Priority field with the Severity value (and just hide the Severity field).

I am looking at the Behaviours Plugin. I've created a Behaviour and using fields Priority and Severity, with Severity as readonly. I've mapped the Behaviour to all issue types and all projects. Below is the serverside script attached to the Priority field.

FormField severity = getFieldById("customfield_11302")
FormField priority = getFieldById("priority")
 
if (severity.getValue() == 1) {
priority.setFormValue("1")
}
else if (severity.getValue() == 2) {
priority.setFormValue("2")
}
else if (severity.getValue() == 3) {
 priority.setFormValue("3")
}
else if (severity.getValue() == 4) {
 priority.setFormValue("4")
}
else if (severity.getValue() == 5) {
 priority.setFormValue("5")
}

This is not working. Any ideas on how to get this working? Or a better method?

Other small changes I've tried is severity.getFormValue() and even enclosing the severity values in "". None have any change.

3 answers

1 accepted

One possible soution (severity is select custom field):

FormField fseverity = getFieldById(fieldChanged)
FormField fpriority = getFieldById("priority")
FormField fpriority0 = getFieldById("priority-field")

String vpriority = (String) fpriority.getFormValue()
String vseverity = (String) fseverity.getFormValue()

// option id for Blocker (from table CUSTOMFIELDOPTION, field ID, you can filter by field CUSTOMFIELD): 12179
// you can also get value by uncoment following line:
// fpriority.setHelpText("Severity:" + vseverity)


if (vseverity == "12175") {
fpriority0.setFormValue("Blocker")
fpriority.setFormValue(1)
}
else if (vseverity == "12176"){
fpriority0.setFormValue("Critical")
fpriority.setFormValue(2)
}

else if (vseverity == "12177"){
fpriority0.setFormValue("Major")
fpriority.setFormValue(3)
}

else if (vseverity == "12178"){
fpriority0.setFormValue("Minor")
fpriority.setFormValue(4)
}
else {
fpriority0.setFormValue("Trivial")
fpriority.setFormValue(5)
}

Thanks Vidic, but I'm not sure that will help my problem. My severity if a scripted field custom field, not select. This is because severity is based on what Impact & Urgency values exist and will change if either one changes. I'm not sure a select cf would accomplish the same?

Workaround: You can also read Impact and Urgeny at the beginning of code and calculate the severity on the same way as it is calculated in scripted field (with combination of "if ... else" or "Switch ... case").

So I tried combining the if...else I used in the scripted field and moved it into the behaviour for the Priority field:

FormField fimpact = getFieldById("customfield_11300")
FormField furgency = getFieldById("customfield_11301")
FormField fpriority = getFieldById("priority")
FormField fpriority0 = getFieldById("priority-field")

String Imp = (String) fimpact.getFormValue()
String Urg = (String) furgency.getFormValue()

if (Imp == "High" && Urg == "High")
{
fpriority0.setFormValue("Critical")
fpriority.setFormValue(1)
} else if (Imp == "High" && Urg == "Medium")
{
fpriority0.setFormValue("Signficiant")
fpriority.setFormValue(2)
} else if (Imp == "High" && Urg == "Low")
{
fpriority0.setFormValue("Major")
fpriority.setFormValue(3)
} else if (Imp == "Medium" && Urg == "High")
{
fpriority0.setFormValue("Signficiant")
fpriority.setFormValue(2)
} else if (Imp == "Medium" && Urg == "Medium")
{
fpriority0.setFormValue("Major")
fpriority.setFormValue(3)
} else if (Imp == "Medium" && Urg == "Low")
{
fpriority0.setFormValue("Important")
fpriority.setFormValue(4)
} else if (Imp == "Low" && Urg == "High")
{
fpriority0.setFormValue("Major")
fpriority.setFormValue(3)
} else if (Imp == "Low" && Urg == "Medium")
{
fpriority0.setFormValue("Important")
fpriority.setFormValue(4)
} else if (Imp == "Low" && Urg == "Low")
{
fpriority0.setFormValue("Trivial")
fpriority.setFormValue(5)
}

But still nothing. I've definetly mapped the behaviour and am unsure why it seems to do nothing.

My upper commment is for custom fields (impact and urgency)

Use custom field option ids (example: "12176") instead of text (example: "Critical") - from table CUSTOMFIELDOPTION, field ID. You can filter these values by field CUSTOMFIELD.

if problem still persist you can also try to change (for impact and urgecy):

from FormField fimpact = getFieldById("customfield_11300")

to FormField fimpact = getFieldByName ("Your field name, maybe Impact")

Another way to get values of custom fields impact and urgency:
Uncomment all except these
FormField fimpact = getFieldByName ("Impact fiel name")
FormField furgency = getFieldByName ("Urgency field name")
FormField fpriority = getFieldById("priority")
FormField fpriority0 = getFieldById("priority-field")
String Imp = (String) fimpact.getFormValue()
String Urg = (String) furgency.getFormValue()
// show current value of impact option id
fimpact.setHelpText("Impact:" + Imp )
// show current value of urgency option id
furgency.setHelpText("Urgency:" + Urg)

Thanks for all the help Vidic.

This is what now works:

FormField fimpact = getFieldByName("Impact")
FormField furgency = getFieldByName("Urgency")
FormField fpriority = getFieldById("priority")
FormField fpriority0 = getFieldById("priority-field")

String Imp = (String) fimpact.getFormValue()
String Urg = (String) furgency.getFormValue()

if (Imp == "10800" && Urg == "10803") {
fpriority.setFormValue("1")
} else if (Imp == "10800" && Urg == "10804") {
fpriority.setFormValue("2")
} else if (Imp == "10800" && Urg == "10805") {
fpriority.setFormValue("3")
} else if (Imp == "10801" && Urg == "10803") {
fpriority.setFormValue("2")
} else if (Imp == "10801" && Urg == "10804") {
fpriority.setFormValue("3")
} else if (Imp == "10801" && Urg == "10805") {
fpriority.setFormValue("4")
} else if (Imp == "10802" && Urg == "10803") {
fpriority.setFormValue("3")
} else if (Imp == "10802" && Urg == "10804") {
fpriority.setFormValue("4")
} else if (Imp == "10802" && Urg == "10805") {
fpriority.setFormValue("5")
}

However, one slight glitchy thing is when I change Impact & Urgency the Priority won't update at first. I have to back into 'Edit' and hit 'Update' again for it to trigger the Priority change.

0 vote

You should add some debug, eg to log the value and type of the severity FormField.

I expect it will be a string, so severity.getFormValue() == 1 won;t work, you should use: severity.getFormValue() == "1"

But you should still add some logging... eg log.warn (...)

I've added in some debug and checked the log. This is what I'm getting:

[onresolve.jira.groovy.GroovyCustomField] javax.script.ScriptException: java.lang.NullPointerException: Cannot invoke method getValue() on null object

Is it just not possible to get the value of the Scripted Field cf?

Just one more comment/opinion:
You have to add code (just in case of priority field): fpriority0.setFormValue("Major") next to fpriority.setFormValue(3)

fpriority.setFormValue(3) change value in issue

fpriority0.setFormValue("Major") change value in current form (custom screen, Edit screen,...)

Suggest an answer

Log in or Sign up to answer
How to earn badges on the Atlassian Community

How to earn badges on the Atlassian Community

Badges are a great way to show off community activity, whether you’re a newbie or a Champion.

Learn more
Community showcase
Published yesterday in Jira Software

How large do you think Jira Software can grow?

Hi Atlassian Community! My name is Shana, and I’m on the Jira Software team. One of the many reasons this Community exists is to connect you to others on similar product journeys or with comparabl...

247 views 4 10
Read article

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you