Restrict the Priority field options

Madhusudhan Matrubai March 19, 2018

Hi There, 
I'm trying to restrict the list of options available under the system field "Priority" but so far no luck. Anyone know how to make this work

 

Here is my code that's not working yet

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

def customFieldManager = ComponentAccessor.getCustomFieldManager()
def optionsManager = ComponentAccessor.getOptionsManager()

def formField = getFieldById(PRIORITY) // def customField = customFieldManager.getCustomFieldObject(formField.getFieldId())
def config = customField.getRelevantConfig(getIssueContext())
def options = optionsManager.getOptions(config)

def optionsMap = options.findAll {
    it.value in ["High", "Medium"] // list of options you want to show}.collectEntries {
    [
        (it.optionId.toString()) : it.value
    ]
}

// or to show the first 12 options//def optionsMap = options.take(12).collectEntries {//    [//        (it.optionId.toString()) : it.value//    ]//}

formField.setFieldOptions(optionsMap)

1 answer

1 accepted

2 votes
Answer accepted
Alexey Matveev
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.
March 19, 2018

Are you sure that your code is executed? Can you see any errors?

Madhusudhan Matrubai March 19, 2018

Thanks @Alexey Matveev for looking into this, I have checked the logs and there is nothing in there.

I'm wondering if it's failing due to this

def config = customField.getRelevantConfig(getIssueContext())
def options = optionsManager.getOptions(config)

since it's not custom field. 

Madhusudhan Matrubai March 19, 2018

Sorry was executing something else, here is what I see after enabling "debug"

user: xxxxxx, fieldId: priority, file: <inline script>
java.lang.NullPointerException: Cannot invoke method getRelevantConfig() on null object
at Script1.run(Script1.groovy:11)

Alexey Matveev
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.
March 19, 2018

The problem is that you use the customField variable but it is not initialized and that is why equals to null. The initialization of the variable is commmented in your script. Kindly uncomment it. 

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

def customFieldManager = ComponentAccessor.getCustomFieldManager()
def optionsManager = ComponentAccessor.getOptionsManager()

def formField = getFieldById(PRIORITY) 
def customField = customFieldManager.getCustomFieldObject(formField.getFieldId()) def config = customField.getRelevantConfig(getIssueContext()) def options = optionsManager.getOptions(config) def optionsMap = options.findAll { it.value in ["High", "Medium"] // list of options you want to show}.collectEntries { [ (it.optionId.toString()) : it.value ] } // or to show the first 12 options//def optionsMap = options.take(12).collectEntries {// [// (it.optionId.toString()) : it.value// ]//} formField.setFieldOptions(optionsMap)

But I think it is better if you follow the guide in Scriptrunner (there is an example how to restrict priorities):

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

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

def formField = getFieldById(PRIORITY)

def optionsMap = ComponentAccessor.getConstantsManager().getPriorityObjects().findAll { priority ->
priority.getName() in ["High", "Medium"]
}.collectEntries {
[(it.id): it.name]
}

formField.setFieldOptions(optionsMap)
softwaremanagement@citadel.com
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!
March 20, 2018

Thanks @Alexey Matveev , I'm sorry it was indeed a bad copy/paste earlier. On my side, it was indeed uncommented. Sorry for that confusion.

BTW when I tried that example, it complains about an deprecated function  "getPriorityObjects()" and hence was using that other route.

Hope that clarifies and once again any help in this matter would be greatly appreciated.

Thanks,

Madhu

Madhusudhan Matrubai March 20, 2018

Thanks @Alexey Matveev , I'm sorry it was indeed a bad copy/paste earlier. On my side, it was indeed uncommented. Sorry for that confusion.

BTW when I tried that example, it complains about an deprecated function  "getPriorityObjects()" and hence was using that other route.

Hope that clarifies and once again any help in this matter would be greatly appreciated.

Thanks,

Madhu

Alexey Matveev
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.
March 20, 2018

Ok. Try like this

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

def formField = getFieldById(PRIORITY)

def optionsMap = ComponentAccessor.getConstantsManager().getPriorities().findAll { priority ->
priority.getName() in ["High", "Medium"]
}.collectEntries {
[(it.id): it.name]
}

formField.setFieldOptions(optionsMap)
softwaremanagement@citadel.com
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!
March 20, 2018

Awesome, that worked like a charm !! 

@Alexey Matveev you are indeed a Champion. :) Thank you so much for taking time to help me with this, totally appreciate it.

Madhusudhan Matrubai March 20, 2018

Awesome, that worked like a charm !! 

@Alexey Matveev you are indeed a Champion. :) Thank you so much for taking time to help me with this, totally appreciate it.

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events