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

Mia
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
April 14, 2013

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

1 vote
Answer accepted
Vidic Florjan
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
April 14, 2013

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)
}

Mia
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
April 15, 2013

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?

Vidic Florjan
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
April 17, 2013

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").

Mia
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
April 17, 2013

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.

Vidic Florjan
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
April 17, 2013

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

Vidic Florjan
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
April 17, 2013

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.

Vidic Florjan
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
April 17, 2013

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")

Vidic Florjan
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
April 17, 2013
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)
Mia
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
April 18, 2013

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 votes
Vidic Florjan
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
April 17, 2013

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,...)

0 votes
JamieA
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
April 14, 2013

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 (...)

Mia
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
April 15, 2013

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?

Suggest an answer

Log in or Sign up to answer