Hi,
I configured a behavior similar to the Dynamic Select behavior in the Adaptavist Library. It works like a charm, basically showing the options for formSelect field based on the value user chooses for formSingleSelect field. This behavior is for jira service desk. The problem I am having is that I haven't figured out how to clear the value of formSelect field if the user chooses a different value for formSingleSelect field. For instance, while creating the request, the user chooses A for formSingleSelect field and AA for formSelect field, and then changes his/her mind and chooses B for formSingleSelect field; at that moment, I need to clear formSelect field since AA still displays on it and it is confusing everybody. How to accomplish that? And thank you in adavance!
Here is the code for the Dynamic Select behavior from the Adaptavist Library:
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)
}