Make custom field required based on priority (Behaviour)

Caleb August 27, 2019

I am trying to make a custom field (Reason for being Critical/Blocker) to be required when the priority is Blocker or Critical.

I am having trouble getting my code to work. I am getting the error [issue] undeclared and I can't seem to get rid of it.

 

Here's my current behaviour script:

import com.atlassian.jira.issue.IssueConstantImpl
import com.onresolve.jira.groovy.user.FieldBehaviours
import groovy.transform.BaseScript
import com.atlassian.jira.issue.Issue

@BaseScript FieldBehaviours fieldBehaviours

def reasonField = getFieldByName("Reason for being Critical/Blocker")

Issue issue = issue

def Priority = issue.getPriority().getName()

if (Priority == 'Blocker' || Priority == 'Critical') {
reasonField.setRequired(true)
} else {
reasonField.setRequired(false)
}

 

 

Any ideas??!

1 answer

0 votes
Marc Minten _EVS_
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.
August 28, 2019

Hmm, I think you are mixing things.

The construction "@Basescript ...." is for REST Endpoints, you are building a behavoiur here ?

Also "issue" is is not defined in the context of behaviours, you should get the contents of the fields by getFieldById or getFieldByName, or access the issue properties by issuecontext.

See the (excellent) examples in the doc !!! https://scriptrunner.adaptavist.com/latest/jira/recipes/behaviours.html

Caleb August 30, 2019

I've tried removing the @BaseScripts and still haven't been able to get the priority status back.

def priority = getFieldById("Priority")

def status = priority.getValue()

 

When going to the logs I see status is returning as null.

 

I even tried:

String status = priority.getValue() as String

 

This still returns null.

 

The only way I can get any data back is when I use :

componentAccessor.getConstantsManger.getPriorities()

 

priorities.getAt(INDEX).getName() is the only way I can get a value but it does not get the selected value since it is index specific

Marc Minten _EVS_
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.
September 2, 2019

Hi,

"Priority" is not the correct "id" for the field Priority, I think it is "priority" (system field id's always start with lowercase!).

so you should write

def priorityField = getFieldById("priority")

or use

def priorityField = getFieldByName("Priority")

 

For more info about how finding the id of a system field, see https://community.atlassian.com/t5/Jira-questions/how-to-find-system-field-id/qaq-p/281440

Caleb September 3, 2019
def priorityField = getFieldById("priority") still returns a null object
Marc Minten _EVS_
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.
September 3, 2019


Do you mean getFieldById("priority") returns a null object ???

or the subsequent getValue() returns null ??

 

I just tried the getFieldById("priority") and it returns me the correct (system) Priority field.

Also getFieldByName("Priority") works...

Caleb September 3, 2019

subsequent getValue() returns null

 

getFieldById("priority") works

Marc Minten _EVS_
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.
September 3, 2019

I see I use in my behaviour scripts everywhere getFormValue() iso getValue() ?

For the priority I use priorityField.getFormValue()?.name to get the priority (as text)

Caleb September 3, 2019

priorityField.getFormValue()?.name is not an option.

 

I don't have any additional options for priorityField.getFomValue()

 

Am I missing a specific import??

scriptrunneroutput.png

Marc Minten _EVS_
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.
September 4, 2019

Hi,

The message you get is just a compiler type check warning (the compiler does not know what object type getFormValue() returns. You can ignore it.

I did some more tests, and indeed, it seems impossible to capture the contents of the priority field. In my existing scripts I was just changing the read-only property of the priority field and this works fine.

This is what I found using a priority field with a value filled in:

FormField priorityField = getFieldById("priority")
log.debug (">priority form value=${priorityField.getFormValue()}")
log.debug (">priority value=${priorityField.getValue()}")
log.debug (">priority field=${priorityField}")

In the logs the getValue() and getFormValue() both return null, when logging the priorityfield itself it also shows ...value=null...

But

FormField priorityField = getFieldById("priority")
priorityField.setReadOnly(true)

correctly sets the priority field read only.

So the field is corerctly identified, but access to the field value is broken.

In the browser debugger I found that also the name (id) "priority-field" was used, so I tried

FormField priorityField = getFieldById("priority-field")
log.debug (">priority form value=${priorityField.getFormValue()}")
log.debug (">priority value=${priorityField.getValue()}")
log.debug (">priority field=${priorityField}")

same results : always returns null, while

FormField priorityField = getFieldById("priority-field")
priorityField.setReadOnly(true)

correctly sets the field to read-only.

So I think there is a bug/incompatibility between scriptrunner and jira. I would suggest you to register this at Adaptavist support...

P.S. we are using an older version of Jira (7.4.3), no idea if this is "resolved" in more recent versions

Caleb September 4, 2019

Thanks for your persistence on this!

We are currently running v8.3.2 and I'm getting the same results (glad it's not just me going crazy!!)

I'll register this at Adaptavist Support and let you know what comes of it

Nataliia Lytvynenko October 18, 2019

@Caled do you have any updates?

 

Because I have the same problem and need to fix it asap!!

Like Oleksii Filiaiev likes this
Subhash Alla December 2, 2020

@Caleb do you have any updates from Adaptavist support , Because I have the same problem

Suggest an answer

Log in or Sign up to answer