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.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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"]]
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
No, this code must map to cf1. This code i test in my environment and it works.
Can you provide screenshot of your configuration?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You re welcome! :)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi I also did the same with my Custom fields and data but the code is not working as expected
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.