Custom field filtering by value from another custom field, multi select required

David Hightower January 8, 2019

@Nic Brough -Adaptavist-

@Jamie Echlin _ScriptRunner - The Adaptavist Group_

I'm looking for javascript or another option (scriptrunner is installed), to allow filtering of custom field B based on custom field A values. I've tried Cascading selects and they don't work for me due to the multi select need on the second custom field.

Custom field B will filter based on the selection of A and allow a selection of multiple values.

A :

  • 1
    • B values:
      • A
      • B
      • C
  • 2
    • B values:
      • D
      • E
      • F
  • 3
    • B values:
      • G
      • H
      • I

Can anyone suggest a method to pull this off, without installing another plugin than scriptrunner?

A and B are required

these are mocked up in html, not as custom fields yet. 

If A:1 is selected, the user can select any combination of the A,B,C values from B. image.png

If A:2 is selected, the user can select any combination of the D, E, F values from B.

image.png

etc..

3 answers

1 accepted

2 votes
Answer accepted
Joanna Choules
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.
January 8, 2019

Hi David,

The recommended approach via ScriptRunner would probably be to use Behaviours. You would want to set up a server-side script on the single-select field which restricts the multi-select field's options using setFieldOptions:

import com.atlassian.jira.component.ComponentAccessor

def singleSelect = getFieldById(fieldChanged)


def
 multiSelect = ComponentAccessor.customFieldManager.getCustomFieldObjectByName("CustomFieldB")
def msConfig = customField.getRelevantConfig(issueContext)
def msOptions = ComponentAccessor.optionsManager.getOptions(msConfig)

def filteredOptions = msOptions

switch (singleSelect.value) {
    case "1":
        filteredOptions = msOptions.findAll {it.value in ['A', 'B', 'C']}
        break

    case "2":
        filteredOptions = msOptions.findAll {it.value in ['D', 'E', 'F']}
        break
}

formField.setFieldOptions(filteredOptions)
David Hightower January 8, 2019

Thanks! @Joanna Choules 

I'm setting up the Behaviour.

I changed customfield.getRelevant... to multiSelect.getRelevant...

and put an actual custom field name in our jira instance  in the getCustomFieldObjectByName("... call

Do I need to define formField as something?
I've tried switching it to multiSelect, but no avail there either. 

 

image.pngimage.png

Joanna Choules
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.
January 8, 2019

Oh yes, sorry, you need to add a line to define a separate FormField for the multi-select:

def formMultiSelect = getFieldByName("CustomFieldB")

and then call setFieldOptions on that. 

David Hightower January 8, 2019

@Joanna Choules Works perfectly! Thanks for your help!

Amar Ghag June 26, 2020

@David Hightower  

It will be nice if you share your full code here.

 

I getting error on line "def multiSelect = ComponentAccessor.customFieldManager.getCustomFieldObjectByName("Alarm")"

 

Thanks,

 Amar

0 votes
Salim Hammar December 22, 2021

Hello everyone 

@Joanna Choules 

The code source it's fonctiannally , but i have a problem beacause when a i change the value for case for example i'm select "1" and after i select "2" the list does not update. 

Exist a function or other for fix this problem ?

For have a list updated when to change case

Thanks in advance :) 

0 votes
Michael Thompson
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.
January 8, 2019

Is it possible to create a new cascading select field to replace your existing fields? I use it and it works great for route problems to the appropriate support teams in our office.

cascadingSelect.png

David Hightower January 8, 2019

@Michael Thompson with the Cascading select, I was limited to a single value selection in the second field. 

I need to be able to select 1 option in first field, then 1 to many in the second field. 

I haven't found a way to get Cascading selects to work for that goal yet. 

Michael Thompson
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.
January 8, 2019

Ahh, I see. In that case this option will not cut it for you. Sorry!

Suggest an answer

Log in or Sign up to answer