Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

ScriptRunner Behaviours: change select list based on another select list option

Neil Blazevic
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
November 19, 2018

Hello,

I have two select lists, Outcomes and Indicators.

Each Outcome and Indicator in the two select lists begine with a series of numbers. 

There are over 100 indicator options, however, only specific indicators relate to a specific Outcome. 

Example would be that 1 Outcome  can relate to 1 Indicator  or 1.1 Indicator or 1.2 Indicator

I'd like to some how filter the results in the Indicator select list based on the Outcome, running off the series of numbers that comes in the beginning of the value. 

If 1 Outcome, only list the matching potential Indicators.

I'm assuming I'll want to accomplish this with ScriptRunner 

 

2 answers

1 vote
Roland Holban (Adaptavist)
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.
November 19, 2018

Create a behaviour on the Outcomes field. This behaviour will change the available options of the Indicators select list every time the Outcomes select list changes. Use the following server-side script:

import com.atlassian.jira.component.ComponentAccessor

def customFieldManager = ComponentAccessor.customFieldManager
def optionsManager = ComponentAccessor.optionsManager

def outcomeField = getFieldById(fieldChanged)
def indicatorsField = getFieldByName("Indicators")

// Get the all the options of Indicators
def indicatorsCF = customFieldManager.getCustomFieldObjectsByName("Indicators")[0]
def config = indicatorsCF.getRelevantConfig(getIssueContext())
def options = optionsManager.getOptions(config)

if (outcomeField.value == "1.0") {
def newOptions = options.findAll { it.value in ["option1", "option2"] }
indicatorsField.setFieldOptions(newOptions)
} else if ((outcomeField.value == "2.0")) {
def newOptions = options.findAll { it.value in ["option3", "option4", "option5"] }
indicatorsField.setFieldOptions(newOptions)
}

Change "1.0" and "option1", "option2", etc. to whatever the names of your options are. You can add more "else if" as you need to. 

Neil Blazevic
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
November 20, 2018

Hi Roland,

Thanks for this, however, I have 75 Outcomes and 130+ indicators, Outcomes may be 1.0, 1.1, 1.2, etc and Indicators may be 1.1.1, 1.1.2, 1.1.3, etc. 

I was looking for a solution that would not require me to hand coded each outcome and indicator into the script. 

Might this be accomplished through a regular expression? 

If user selected Outcome 1.1, then only show Indicators that match on 1.1.*

Roland Holban (Adaptavist)
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.
November 20, 2018

You could do something like this. It would eliminate the need for hardcoding all the Indicators, but you would still need to hardcode the Outcomes. It would more or less be copy-pasting the else if and changing the two numbers.

if (outcomeField.value == "1.0") {
def newOptions = options.findAll { it.value.startsWith("1.0") }
indicatorsField.setFieldOptions(newOptions)
} else if ((outcomeField.value == "1.1")) {
def newOptions = options.findAll { it.value.startsWith("1.1") }
indicatorsField.setFieldOptions(newOptions)
}

 

Sylvain Leduc
Contributor
September 28, 2020

Thanks @Roland Holban (Adaptavist)  !
But how to not only show value in a multiple select list, but select/highlight these values please ?

Amos
Contributor
October 16, 2023

How would this work if the options were asset (insight) objects? I know that you can established reference asset object custom fields, however if I hypothetically wanted to control this behaviour in a ScriptRunner field behaviour, is it possible?

Like Michael Agius Muscat likes this
0 votes
Jaswanth P
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.
June 11, 2020

How to write something like below? I want to show all options but a few selected ones.

options.findAll { it.value NOT in
Javier Pérez November 19, 2020

Hi, 

You can use every function like this:

options.findAll { it.every{ it != 'Your option to avoid'} }

Suggest an answer

Log in or Sign up to answer