Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
4,366,738
Community Members
 
Community Events
168
Community Groups

Limit Options in Multi-Select List with Behavior

Hi everyone,

I'm trying to limit the options of a multi select list based on a choice that's been made in a single select list via Behaviors. 

So the single select listA has values of A, AB and the multi-select listB has values Monday, Tuesday, Wednesday

If single select listA= A, I want multi-select listB to show the values Monday, Wednesday

If single select listA= AB, I want multi-select listB to show the values Monday, Tuesday

Any Ideas how to archive this? 

thanks in advance for any help 

1 answer

1 accepted

0 votes
Answer accepted

Hi Melanie

Welcome to the community :)

There are dozens of such examples in the community, but here is how I would do it.

def formFieldA = getFieldByName('fieldA')
def formFieldB = getFieldByName('fieldB')
def map = [
'A':['Monday', 'Wednesday'],
'AB':['Monday', 'Tuesday']
]

def cfFieldB = customFieldManager.getCustomFieldObject(formFieldB.fieldId)
def config = cfFieldB.getRelevantConfig(issueContext)
def allOptions = optionsManager.getOptions(config)

def optionsToAllow = allOptions.findAll{ option->
map[formFieldA.value].contains(option.value)
}
formFieldB.setFieldOptions(optionsToAllow)

This script should be applied to the server-side script for FieldA. This will make the script run each time fieldA is changed.

Writing it this way makes it easier to copy to other scenarios and/or add new option mapping in the future. 

Hi @Peter-Dave Sheehan 

thanks so much for your help. Really appreciate it! 🙏 

I applied it to my fieldA but I'm getting two errors, not sure if I can just ignore them or if its something that must be fixed to have it work properly.. 

-> def allOptions = optionsManager.getOptions(config)
variable is undeclared 

-> map[formFieldA.value].contains(option.value)
no such property: value for class: java.lang.Object

Could you have a look at it and tell me, what needs to be changed/added here? 


Ah... sorry, I wasn't careful with the details.

Add the following 3 lines a tthe top of the script:

import com.atlassian.jira.component.ComponentAccessor
def optionsManager = ComponentAccessor.optionsManager
def customFieldManager = ComponentAccessor.customFieldManager
Like Melanie Nitter likes this

Did it. No errors now. But it's only working for my first mapping. 
So selecting A shows the multi-select with the options I want but selecting B shows nothing at all, the multi-select field is not showing up. 

Make sure the spelling for B matches the first part in my example

def map = [
'A':['Monday', 'Wednesday'],
'AB':['Monday', 'Tuesday']
]

I took you literally and labeled the second option "AB". If your second label is B and not AB, then your map will look like this:

def map = [
'A':['Monday', 'Wednesday'],
'B':['Monday', 'Tuesday']
]

 Beyond that, I'll need to see your full script.

import com.atlassian.jira.component.ComponentAccessor
def optionsManager = ComponentAccessor.optionsManager
def customFieldManager = ComponentAccessor.customFieldManager

def formFieldA = getFieldByName('Zielgruppe')
def formFieldB = getFieldByName('Tätigkeit')
def map = [
'Büropersonal':["Arbeitszeitmanager/-in","Betriebsdisponent/-in","Betriebsmanager", "Fahr-, Dienst- und Umlaufplaner/-in","Koordinator Kundendialog", "Leiter Fahrgastmarketing", "Leiter/-in Niederlassung" ,"Marktmanager","Mitarbeiter Fahrtwunschzentrale", "Personaldisponent/-in","Referent Fahrgastmarketing", "Referent Kommunikation", "Sachbearbeiter Abomanagement","Sachbearbeiter Kundencenter","Sachbearbeiter Kundendialog","Teamleiter Abomanagement","Teamleiter Fahrdienst","Teamleiter Fahrtwunschzentrale","Teamleiter Kundencenter","Teamleiter Leistelle","Teamleiter Werkstatt"],
'Fahrpersonal':["Busfahrer","Teamleiter Fahrdienst"]
]

def cfFieldB = customFieldManager.getCustomFieldObject(formFieldB.fieldId)
def config = cfFieldB.getRelevantConfig(issueContext)
def allOptions = optionsManager.getOptions(config)

def optionsToAllow = allOptions.findAll{ option->
map[formFieldA.value].contains(option.value)
}
formFieldB.setFieldOptions(optionsToAllow)

Thats how it looks. The Values I gave you were just examples because, as you can see, there are a lot of values I want to show

I don't see anything inherently wrong there.

You can add something like this to give you immediate clues:

formFieldA.setHelpText("Detected value $formFieldA.value (${formFieldA.value.getClass()}) in $formFieldA.fieldId this maps to ${map[formFieldA.value]}")

 That might provide some clues. Maybe there is a slight spelling diference.

Okay, added this to my script and I noticed something odd while running it.
Selecting A gets me the multi-select and some info what options are mapped to A. 
Selecting B only gets me the info about the mapped options, but no multi-select.
And selecting another option C in FieldA shows me the info about the mapped options even tough I haven't even mapped them. But it says, that the options from B are also mapped to C. I made some screenshots so it might be more clear.. except the lines you posted last I havent changed anything in the script. Bildschirmfoto 2021-02-10 um 07.28.39.pngBildschirmfoto 2021-02-10 um 07.28.54.pngBildschirmfoto 2021-02-10 um 07.28.28.png

Okay, added this to my script and I noticed something odd while running it.
Selecting A gets me the multi-select and some info what options are mapped to A. 
Selecting B only gets me the info about the mapped options, but no multi-select.
And selecting another option C in FieldA shows me the info about the mapped options even tough I haven't even mapped them. But it says, that the options from B are also mapped to C. I made some screenshots so it might be more clear.. except the lines you posted last I havent changed anything in the script. Bildschirmfoto 2021-02-10 um 07.28.28.pngBildschirmfoto 2021-02-10 um 07.28.39.pngBildschirmfoto 2021-02-10 um 07.28.54.png

When the helpText doesn't change after you change the single-select, I suspect there is some error happening in the code. If you have access to your <jira-home>/log/atlassian-jira.log I suspect you will find some additional clues there.

But given that you have unmapped options that I assume you want to default to showing all options in that case.

You can try this

import com.atlassian.jira.component.ComponentAccessor
def optionsManager = ComponentAccessor.optionsManager
def customFieldManager = ComponentAccessor.customFieldManager

def formFieldA = getFieldByName('Zielgruppe')
def formFieldB = getFieldByName('Tätigkeit')
def map = [
'Büropersonal':["Arbeitszeitmanager/-in","Betriebsdisponent/-in","Betriebsmanager", "Fahr-, Dienst- und Umlaufplaner/-in","Koordinator Kundendialog", "Leiter Fahrgastmarketing", "Leiter/-in Niederlassung" ,"Marktmanager","Mitarbeiter Fahrtwunschzentrale", "Personaldisponent/-in","Referent Fahrgastmarketing", "Referent Kommunikation", "Sachbearbeiter Abomanagement","Sachbearbeiter Kundencenter","Sachbearbeiter Kundendialog","Teamleiter Abomanagement","Teamleiter Fahrdienst","Teamleiter Fahrtwunschzentrale","Teamleiter Kundencenter","Teamleiter Leistelle","Teamleiter Werkstatt"],
'Fahrpersonal':["Busfahrer","Teamleiter Fahrdienst"]
]

def cfFieldB = customFieldManager.getCustomFieldObject(formFieldB.fieldId)
def config = cfFieldB.getRelevantConfig(issueContext)
def allOptions = optionsManager.getOptions(config)
def optionsToAllow
def selected = formFieldA.value

if(selected in map.keySet()){
formFieldA.setHelpText("Detected mapped value $formFieldA.value in ${formFieldA.fieldId}. Multi-select will be limited to ${map[formFieldA.value]}")
optionsToAllow = allOptions.findAll{ option->
map[formFieldA.value].contains(option.value)
}
} else {
formFieldA.setHelpText("Detected unmapped value $formFieldA.value in ${formFieldA.fieldId}. Multi-select will include all options")
optionsToAllow = allOptions
}
formFieldB.setFieldOptions(optionsToAllow)

I was able to find the reason why it wasn't working. Another plugin was the reason. Now everything works perfectly :) Thank you so much for you help and patience!  

Happy to help.

Glad you figured it out. :)

Suggest an answer

Log in or Sign up to answer
TAGS

Atlassian Community Events