How to restrict Priority regarding issue type

Guillaume MACKOWIAK January 12, 2022

Bonjour, 

In Project PROJ1, there are

- priority scheme listed as Low, Medium, High

- issue type scheme request and bug

 

I would to create a Behavior to restrict the available list of Prorities regarding issue type

Request would be proposed Low, Medium 

Bug would be proposed Medium and High

How could I achieve this° 

Regards, 

Guillaume

 

3 answers

3 votes
Vikrant Yadav
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
January 12, 2022

Bonjour @Guillaume MACKOWIAK If this is system Priority field you have to apply Script Runner Behaviour on "Issue Type" and add the following Server Side Script :- 

 

import com.atlassian.jira.component.ComponentAccessor

import static com.atlassian.jira.issue.IssueFieldConstants.PRIORITY

def constantsManager = ComponentAccessor.getConstantsManager()
def formField = getFieldById(PRIORITY)

if (issueContext.issueType.name == "Bug") {
def optionsMap = ComponentAccessor.getConstantsManager().getPriorities().findAll { priority ->
priority.getName() in ["Major", "Minor"]
}.collectEntries {
[(it.id): it.name]
}
formField.setFieldOptions(optionsMap)
} else {
def optionsMap = ComponentAccessor.getConstantsManager().getPriorities().findAll { priority ->
priority.getName() in ["Minor", "Trivial"]
}.collectEntries {
[(it.id): it.name]
}
formField.setFieldOptions(optionsMap)
}

 https://scriptrunner.adaptavist.com/4.3.2/jira/recipes/behaviours/restricting-priority-and-resolution.html

sChupin January 20, 2022

Thank you very much ! It worked fine for me.

However, is it possible to change the current priority when I change the issue type in the issue creation screen ?

I would like to prevent a restricted priority to be set to an issue by changing its type after settings the priority.

For example something like this :

when issueType changes
  if (issueType == Bug)
    priority = "high"
  else
    priority = "medium" // because other issue types don't have the high priority

I tried this :

import com.atlassian.jira.component.ComponentAccessor
import static com.atlassian.jira.issue.IssueFieldConstants.PRIORITY

def formField = getFieldById(PRIORITY)

if (getActionName() in ["Create Issue","Create"]) {
if (issueContext.issueType.name == "Bug") {
formField.setFormValue("High")
} else {
formField.setFormValue("Medium")
}
}

but then I can't change the priority anymore as it resets it all the time to the default value.

How can I execute the script only when the issue type is changed ? 

Vikrant Yadav
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
January 20, 2022

Hi @sChupin Try below script :- 

Except Bug issue type, value come back to "Major".

Kindly apply Behaviour on Issue Type field.

import com.atlassian.jira.component.ComponentAccessor

import static com.atlassian.jira.issue.IssueFieldConstants.PRIORITY

def constantsManager = ComponentAccessor.getConstantsManager()
def formField = getFieldById(PRIORITY)

if (issueContext.issueType.name == "Bug") {
formField.setFormValue("Blocker")
formField.setReadOnly(true)
} else {
formField.setFormValue("Major")// set value as per your requirement
formField.setReadOnly(true) //make field read only
}

 Thanks

V.Y

0 votes
Hosein Norouzi
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
January 31, 2024

hi @Vikrant Yadav 

i used the code you suggest but i face following error:


[Static type checking] - Cannot call com.onresolve.jira.groovy.user.FormField#setFieldOptions(java.util.Map<?, java.lang.String>) with arguments [java.util.Map<java.lang.Object, java.lang.Object>]


this error is related to the code line :

*formField.setFieldOptions(optionsMap)*



except this theres no error on other lines of code . how can i fix this? 
thanks

Antreas Solou February 13, 2024

Hi @Hosein Norouzi 

You can try this one, it should work. I had the same issue as you. 

import com.atlassian.jira.component.ComponentAccessor

import static com.atlassian.jira.issue.IssueFieldConstants.PRIORITY

def constantsManager = ComponentAccessor.getConstantsManager()
def formField = getFieldById(PRIORITY)

if (issueContext.issueType.name == "Bug") {
def optionsMap = ComponentAccessor.getConstantsManager().getPriorities().findAll { priority ->
priority.getName() in ["Medium", "Critical"]
}.collectEntries {
[(it.id.toString()): it.name.toString()]
}
formField.setFieldOptions(optionsMap as Map<String, String>)
} else {
def optionsMap = ComponentAccessor.getConstantsManager().getPriorities().findAll { priority ->
priority.getName() in ["Medium", "Low"]
}.collectEntries {
[(it.id.toString()): it.name.toString()]
}
formField.setFieldOptions(optionsMap as Map<String, String>)
}

Like Vikrant Yadav likes this
0 votes
Pelin Türe March 23, 2022

Hi @Vikrant Yadav

 

I want to ask something. 

This behaviour works fine in issue create screen, but when i change the issue type in edit screen it's not working. Do you have any suggestions regarding this situation? Is there any way to solve this without listener, just using behaviour?

 

Thanks in advance

Vikrant Yadav
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
March 23, 2022

Hi @Pelin Türe It should work on edit screen as well. Are you changing issue type from view screen or from edit screen ? On view screen it won’t work. 
Is behaviour applied on Issue type ? 

thanks 

V.Y

Pelin Türe March 23, 2022

Thank you for your answer. Yes, I did it as you said by adding the issue type field.

A single screen is used for edit, view and create operations, do you think that might be the reason?

Suggest an answer

Log in or Sign up to answer