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