Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Limit Options in Multi-Select List with Behavior

Melanie Nitter February 8, 2021

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

Suggest an answer

Log in or Sign up to answer
0 votes
Answer accepted
Peter-Dave Sheehan
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
February 8, 2021

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. 

Melanie Nitter February 8, 2021

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? 


Peter-Dave Sheehan
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
February 8, 2021

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
Melanie Nitter February 8, 2021

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. 

Peter-Dave Sheehan
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
February 8, 2021

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.

Melanie Nitter February 8, 2021
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

Peter-Dave Sheehan
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
February 8, 2021

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.

Melanie Nitter February 9, 2021

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

Melanie Nitter February 10, 2021

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

Peter-Dave Sheehan
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
February 10, 2021

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)
Melanie Nitter February 11, 2021

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!  

Peter-Dave Sheehan
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
February 11, 2021

Happy to help.

Glad you figured it out. :)

TAGS
AUG Leaders

Atlassian Community Events