It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

Make custom field required based on priority (Behaviour)

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

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

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

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

def priorityField = getFieldById("priority") still returns a null object


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

subsequent getValue() returns null

 

getFieldById("priority") works

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)

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

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

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

@Caled do you have any updates?

 

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

Like Oleksii Filiaiev likes this

Suggest an answer

Log in or Sign up to answer
Community showcase
Posted in Jira

Demo Den Ep. 7: New Jira Cloud Reports

Learn how to use two new reports for next-gen projects in Jira Cloud:  Cumulative flow diagram and Sprint burndown chart. Ivan Teong, Product Manager, Jira Software, demos the Cumulative ...

314 views 1 3
Join discussion

Community Events

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

Find an event

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

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you