Skip to main content
Example scripts
arrow icon
To homepage
Jira
Data centre icon
Data Center

Dynamic Select

Features
Behaviours
Created 1 year ago, Updated 0 day(s) ago
App in script
ScriptRunner For Jira
ScriptRunner For Jira
by Adaptavist
Compatibility
compatibility bullet
Jira (8.0 - 8.14)
compatibility bullet
ScriptRunner For Jira (6.18.0)
Language |
groovy
import com.atlassian.jira.component.ComponentAccessor
import com.onresolve.jira.groovy.user.FieldBehaviours
import groovy.transform.BaseScript

@BaseScript FieldBehaviours fieldBehaviours

final singleSelectName = 'Some Single Select'
final selectName = 'Other Single or Multi Select'

def cfManager = ComponentAccessor.customFieldManager
def optionsManager = ComponentAccessor.optionsManager

def singleSelectField = cfManager.getCustomFieldObjectsByName(selectName)[0]
def formSingleSelect = getFieldByName(singleSelectName)
def singleSelectValue = formSingleSelect?.value
def formSelect = getFieldByName(selectName)

// Make sure the fields we want to work with are on the form
if (!formSingleSelect && !formSelect) {
    return
}

// Must grab the relevant config given the current issue context and the custom field
def config = ComponentAccessor.fieldConfigSchemeManager.getRelevantConfig(issueContext, singleSelectField)
// Given config, grab a map of the possible options for the second select custom field
def options = optionsManager.getOptions(config)

// Define your preset options for each single select case using the format shown here.
// Just put the values, not the keys/IDs.
def optionSet1 = [
    'Option 1',
    'Option 2',
    'Option 3',
    'Option 4'
]
def optionSet2 = [
    'Option 2',
    'Option 3',
    'Option 4'
]
def optionSet3 = [
    'Option 2',
    'Option 4'
]

// Make sure the second field actually has options to set
if (!options) {
    return
}

// Set/use the appropriate optionSet, dependent on the value of the currently selected single select option
switch (singleSelectValue) {
// Notice: The optionSet that is used is changed in each case
// Change 'Single Select Option...' to match your single select's values.
    case 'Single Select Option 1':
        formSelect.setFieldOptions(options.findAll { it.value in optionSet1 }.collectEntries {
            [(it.optionId): it.value]
        })
        break
    case 'Single Select Option 2':
        formSelect.setFieldOptions(options.findAll { it.value in optionSet2 }.collectEntries {
            [(it.optionId): it.value]
        })
        break
    case 'Single Select Option 3':
        formSelect.setFieldOptions(options.findAll { it.value in optionSet3 }.collectEntries {
            [(it.optionId): it.value]
        })
        break
// Reset to default options if single select option is null or any other option that is not taken care of
    default:
        formSelect.setFieldOptions(options)
}
Having an issue with this script?
Report it here