Script Runner Behavior - Cascade drop down list based on previous field selection

Sravya Vuggina June 21, 2018

I have created 2 custom fields (CF1 and CF2) with list of values. I would like to populate cascading values for selection in CF2 based on value selected in CF1.

Example: CF1 contains values A, B, C. CF2 contains values A1, A2, A3, B1, B2, B3, C1, C2 and C3. I would like to display only A1, A2 and A3 in CF2 when A is selected in CF1. 

1 answer

1 accepted

1 vote
Answer accepted
Mark Markov
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 21, 2018

Hello @Sravya Vuggina

For this case you can use  Select list(cascading) field type that goes out of the box.

But if you still need behaviour for some reason, you can write code like this (for your example):

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.customfields.manager.OptionsManager
import com.atlassian.jira.issue.customfields.option.Options
import com.atlassian.jira.issue.fields.config.FieldConfig

def cf1 = getFieldById(getFieldChanged())
def cf2 = getFieldByName('CF2')
def customField = getCustomFieldManager().getCustomFieldObjectByName("CF2")
OptionsManager optionsManager = ComponentAccessor.getOptionsManager()
FieldConfig config = customField.getRelevantConfig(underlyingIssue)
Options options = optionsManager.getOptions(config)
cf2.setFieldOptions(options.findAll {
//HERE MUST BE YOU LOGIC
it.value.substring(0,1) == cf1.getValue().toString().substring(0,1)
}) 

It must be mapped to your CF1 field

To put it simply, all magic happens here

it.value.substring(0,1) == cf1.getValue().toString().substring(0,1)

This means values for CF2 will starts with first character in CF1,  

Sravya Vuggina June 21, 2018

Hi @Mark Markov,

Apologies for the vague description, values like A1 A2 was just an example. In my case first character logic cannot be used as the values are entirely different. Maybe a place holder to mention list of CF2 values that need to be shown for a specific CF1 value selected will help. 

Mark Markov
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 21, 2018

For example you can use Map variable to directly declare values in cf2 like this

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.customfields.manager.OptionsManager
import com.atlassian.jira.issue.customfields.option.Options
import com.atlassian.jira.issue.fields.config.FieldConfig


Map<String, List<String>> logic = ["A" : ["AA1","AA2", "AA3"], "B": ["BB1","BB2","BB3"], "C":["CC1", "CC2", "CC3"]]
def cf1 = getFieldById(getFieldChanged())
def cf2 = getFieldByName('CF2')
def customField = getCustomFieldManager().getCustomFieldObjectByName("CF2")
OptionsManager optionsManager = ComponentAccessor.getOptionsManager()
FieldConfig config = customField.getRelevantConfig(underlyingIssue)
Options options = optionsManager.getOptions(config)
cf2.setFieldOptions(options.findAll {
//HERE MUST BE YOU LOGIC
it.value in logic.get(cf1.getValue().toString())
}) 

 

Where "A" you cf1 value and ["AA1","AA2"] list of values that must be in cf2 when "A" is choosen

Map<String, List<String>> logic =  ["A" : ["AA1","AA2", "AA3"], "B": ["BB1","BB2","BB3"], "C":["CC1", "CC2", "CC3"]]
Sravya Vuggina June 21, 2018

I have applied this code to CF1 field (changed the values as per my requirement).  Somehow, it doesn't seem to perform cascading.  Just out of curiosity I have tried applying to CF2 field instead of CF1 that dint work either. 

While I was browsing through existing questions I ran into this code. Firstly FormField wasn't getting detected so replaced that with def, yet dint cascade. 

Am I missing some obvious steps? 

Mark Markov
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 21, 2018

No, this code must map to cf1. This code i test in my environment and it works.

Can you provide screenshot of your configuration?

Sravya Vuggina June 21, 2018

Please find the screen shots below.Mapping.pngCode.pngDropdownlist.png

Mark Markov
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 22, 2018

Hello @Sravya Vuggina

It seems something happen on the form. Can you look in atlassian-jira.log when you open form, may be there are some errors that helps to understand what happens.

Sravya Vuggina June 22, 2018

Unfortunately, there is nothing unusual in the log. Seems to be quite an easy thing to do, but I am having hard time achieving it.

Sravya Vuggina June 25, 2018

Hello @Mark Markov,

Thanks for taking time and writing up the code. It worked like a champ; however, I had to replace "underlyingIssue" with "getIssueContext()" in the below mentioned line. I was trying to tweak the code based on some online material, and it worked (Not that I know of the difference! ).  

FieldConfig config = customField.getRelevantConfig(underlyingIssue)

 Curious to know what "underlyingIssue" was intended to do. 

Mark Markov
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 25, 2018

Hmmm, I forgot about that :)

underlyingIssue is current Issue object and it seems you have these field on create screen, so Issue object doesnt exist and thats why it is didnt work

https://docs.atlassian.com/software/jira/docs/api/7.2.3/com/atlassian/jira/issue/Issue.html

IssueContext its global current scope that not depends on Issue

https://docs.atlassian.com/software/jira/docs/api/7.2.3/com/atlassian/jira/issue/context/IssueContext.html

Sravya Vuggina June 25, 2018

Appreciate all your help! :) 

Mark Markov
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 25, 2018

You re welcome! :)

Kevin Johnson
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
July 18, 2019

Hi I also did the same with my Custom fields and data but the code is not working as expected

Dinesh September 23, 2019

Hi Guys,

I am trying to hide "None" option in custom field "Reassign reason" , Please let me know any suggestions. please find attached.

select list should hide field - None.pngl

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events