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

It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

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

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

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,  

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. 

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"]]

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? 

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

Can you provide screenshot of your configuration?

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

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.

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.

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. 

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

Appreciate all your help! :) 

You re welcome! :)

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

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
Community showcase
Published in Marketplace Apps & Integrations

5 mobile apps for Jira Cloud to boost productivity

  It’s very important to have access to the workflow process from anywhere. Especially if you manage the work of others. There is no difference whether you’re out of office, or drive a ca...

216 views 2 5
Read article

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you