Filter values from cascading select based on selection from other single select field

Peter Newton April 6, 2022

Hi,

Is it possible to filter the shown values in a cascading select based on a single select selection?

for example

I have a field called Topic that is a single select with values 1, 2 and 3

I have a cascading select field called Product with parents:

A and child A1,B2,B3

B and child B1,B2,B3

C and child C1,C2,C3

I need to hide parent B and C when I select 1 from field Topic

When I select 2 from Topic I need to hide parents A and B

 

Thanks

2 answers

1 accepted

0 votes
Answer accepted
Ram Kumar Aravindakshan _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
April 8, 2022

Hi @Peter Newton

For your requirement, you can try something like this:-

import com.atlassian.jira.component.ComponentAccessor
import com.onresolve.jira.groovy.user.FieldBehaviours
import groovy.transform.BaseScript

@BaseScript FieldBehaviours behaviours
def customFieldManager = ComponentAccessor.customFieldManager
def optionsManager = ComponentAccessor.optionsManager

def sampleList = getFieldById(fieldChanged)
def sampleListValue = sampleList.value.toString()
def sampleCascade = getFieldByName('Sample Cascade')

def filteredCascadeField = customFieldManager.getCustomFieldObject(sampleCascade.fieldId)
def availableConfig = filteredCascadeField.getRelevantConfig(issueContext)
def listCascadeOptions = optionsManager.getOptions(availableConfig)

def childField = getFieldById("${sampleCascade.fieldId}:1")
def childList = customFieldManager.getCustomFieldObject(childField.fieldId)
def msConfig = childList.getRelevantConfig(issueContext)
def childOptions = optionsManager.getOptions(msConfig)

def availableOptions

if (sampleListValue == '1') {
availableOptions = listCascadeOptions.findAll {it.value in ['A'] }.collectEntries {[(it.optionId.toString()) : it.value] }
} else if (sampleListValue == '2') {
availableOptions = listCascadeOptions.findAll {it.value in ['B'] }.collectEntries {[(it.optionId.toString()) : it.value] }
} else if (sampleListValue == '3') {
availableOptions = listCascadeOptions.findAll {it.value in ['C'] }.collectEntries {[(it.optionId.toString()) : it.value] }
}

sampleCascade.setFieldOptions(availableOptions)
sampleCascade.setFormValue([availableOptions, childOptions.optionId])

Please note that the sample working above is not 100% exact to your environment. Hence, you will need to make the required modifications.

The code above is for a Server-Side Behaviour, i.e. for the Single Select list that you want to use to filter the Cascading List.

This Server-Side Behaviour filters the Sample Cascade field when the value from the Sample List field is selected.

Below is a print screen of the Behaviour configuration:-

behaviour_config.png

Also, I include a few test print screens for your reference:-

1. When I select option 1 from the Sample List, the Sample Cascade list is filtered to only option A and its child list will only show the options A1, A2 and A3, as shown in the print screens below:-

1.png2.png

 

2. When I select option 2 from the Sample List, the Sample Cascade list is filtered to only option B, and its child list will only show the options B1, B2 and B3 as shown in the print screens below:-

3.png4.png

 

3. Lastly, when I select option 3 from the Sample List, the Sample Cascade list is filtered to only option C, and its child list will only show the options C1, C2 and C3, as shown in the print screens below:-

5.png6.png

I hope this helps to solve your question. :)

Thank you and Kind regards,

Ram

MohanBabu Somasundaram November 8, 2022

Hi Team,

If I select C1 from Child need a new select list field called Test to be enabled, Please help me with a Groovy script or by options.

Bohdan Lozinskyi April 5, 2023

Hi @Ram Kumar Aravindakshan _Adaptavist_ ,

How to show only A1,B1,C1 child values in your example?

Thanks!

Ram Kumar Aravindakshan _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
April 9, 2023

Hi @Bohdan Lozinskyi

I would assume that for A, you want A1, B, it's B1, and C, C1.

For this, you will have only to modify the code slightly:-

import com.atlassian.jira.component.ComponentAccessor
import com.onresolve.jira.groovy.user.FieldBehaviours
import groovy.transform.BaseScript

@BaseScript FieldBehaviours behaviours
def customFieldManager = ComponentAccessor.customFieldManager
def optionsManager = ComponentAccessor.optionsManager

def sampleList = getFieldById(fieldChanged)
def sampleListValue = sampleList.value.toString()
def sampleCascade = getFieldByName('Sample Cascade')

def filteredCascadeField = customFieldManager.getCustomFieldObject(sampleCascade.fieldId)
def availableConfig = filteredCascadeField.getRelevantConfig(issueContext)
def listCascadeOptions = optionsManager.getOptions(availableConfig)

def childField = getFieldById("${sampleCascade.fieldId}:1")
def childList = customFieldManager.getCustomFieldObject(childField.fieldId)
def msConfig = childList.getRelevantConfig(issueContext)
def childOptions = optionsManager.getOptions(msConfig)

def availableOptions

if (sampleListValue == '1') {
availableOptions = listCascadeOptions.findAll {it.value == 'A1' }.collectEntries {[(it.optionId.toString()) : it.value] }
} else if (sampleListValue == '2') {
availableOptions = listCascadeOptions.findAll {it.value == 'B1' }.collectEntries {[(it.optionId.toString()) : it.value] }
} else if (sampleListValue == '3') {
availableOptions = listCascadeOptions.findAll {it.value == 'C1' }.collectEntries {[(it.optionId.toString()) : it.value] }
}

sampleCascade.setFieldOptions(availableOptions)
sampleCascade.setFormValue([availableOptions, childOptions.optionId])

This should work for your requirement.

Thank you and Kind regards,

Ram 

0 votes
MohanBabu Somasundaram November 8, 2022

Hi, Team if c1 is selected from the child I need to display a new selectlist field called Test select list, Please help me with a script or option.

Suggest an answer

Log in or Sign up to answer