We can use Behaviours in order to hide options of a customfield/dropdown in Jira depending of the value of the IssueType. If you are new I recommend first to do this example: Show/Hide Mandatory fields in Jira dynamically with Behaviours

Behaviours is part of the Scriptrunner plugin of Adaptavist.

In this example we will observe two pieces of code, one to restrict the options of the System field of Jira called “Priority” and another one to restrict the options of some customfield typical dropdown.

In the example below you can see how to restrict the options of the Priority field accepting only the Priorities with ID lower than 6. And the script set the default value to the option with ID: 3. There are some lines commented if you want to restrict the script by Jira group.

 


import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.sal.api.user.UserManager

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

def constantsManager = ComponentAccessor.getConstantsManager()

def userUtil = ComponentAccessor.getUserUtil()
log.debug ComponentAccessor.getComponent(UserManager)

//def currentUser = ComponentAccessor.getJiraAuthenticationContext().getUser()
//if (! userUtil.getGroupNamesForUser(currentUser.name).contains("jira-developers")) {

    def allowedPriorities = constantsManager.getPriorityObjects().findAll {
        it.id.toInteger() < 6
    }.collectEntries {
        [(it.id): it.name]
    }

    getFieldById(PRIORITY).setFieldOptions(allowedPriorities)
    getFieldById(PRIORITY).setFormValue("3")
//}

  In other side, in this other example we obtain a typical dropdown field using its name (FieldName) and we restrict it's values/options for some fixed ones (A,B,C,D).  

import com.atlassian.jira.component.ComponentAccessor

def singleSelect = getFieldById(getFieldChanged())
def optionsManager = ComponentAccessor.getOptionsManager()

def cf = getFieldByName("FieldName")
def cfField = customFieldManager.getCustomFieldObject(cf.getFieldId())
def cfConfig = cfField.getRelevantConfig(getIssueContext())
def cfOptions = optionsManager.getOptions(cfConfig)
def cfA = cfOptions.findAll { it.value in ["A","B","C","D"] }.collectEntries { [ (it.optionId.toString()) : it.value ] }
cf.setFieldOptions(cfA)

 

As you know we can restrict the code by IssueType using the standard configuration of the Behaviour ( is the mapping of the code to the Project/IssueTypes part). Hope this trick will be useful for all of you!

By MrAddon

Posted by:.

5 replies on “How to hide options of a customfield depending on the IssueType

  1. Since JIRA doesn’t have a Scheme for Issue Link, I was thinking about creating a behavior for this.

    But I am not able to identify the link issue field to filter the options.

    Can you tell me if this would be possible?

    Like

  2. Hello,
    Is there any way to hide the system default ‘None’ value from a select list on the Create and Edit issue screen on JIRA Server v8.22.6?
    I tried various methods using the behaviours, however was not successful.
    Here is an example:

    import com.atlassian.jira.component.ComponentAccessor
    import com.atlassian.jira.issue.customfields.manager.OptionsManager
    def optionsManager = ComponentAccessor.getComponent(OptionsManager)

    def method = getFieldByName(“Decision”)
    method.setAllowInlineEdit(false)

    def methodcustomField = customFieldManager.getCustomFieldObject(method.getFieldId())
    def methodconfig = methodcustomField.getRelevantConfig(getIssueContext())
    def methodOptionsOriginal = optionsManager.getOptions(methodconfig)

    /*define the select list manually, do not include the ‘none’*/
    def methodOptionsCustYes = methodOptionsOriginal.findAll { it.value in [‘NA’, ‘Accept’, ‘Cancel’, ‘Mitigate’, ‘Transfer’] }

    method.setFieldOptions( methodOptionsCustYes )

    Regards,
    Rahul

    Like

    1. Hello Rahul,

      To remove the “none” option usually you need to add a “Default” value to the customfield (in the standard admin customfield page).

      Regards!

      Like

  3. Got totally different problem, when apply mentioned behavior, my selection is kept with mentioned values, but even NONE disappear from options, which is not needed.
    If I try to add none to it.value it wont help 😀
    Any idea?

    Like

Leave a comment