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

how to get the Cascading select value

ION Information Systems December 17, 2012

Hello, in Jira 4 I need to write a Groovy script to get the selected value in a cascading select custom field.

for example I have:

Customfield: 1,2,3

Cascading field: a,b,c

If I select on the interface 1,a I need programmatically to get both selection.

How can I acheive that?

thanks,

3 answers

1 accepted

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

5 votes
Answer accepted
RambanamP
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.
December 18, 2012

try with following code

Object value = issue.getCustomFieldValue(customField);
CustomFieldParams params = (CustomFieldParams) value;
if (params != null) {
Object parent = params.getFirstValueForNullKey();
Object child = params.getFirstValueForKey("1");
}

ION Information Systems December 18, 2012

it worked. Thanks!

RambanamP
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.
December 18, 2012

if it works accept as a correct answer! :)

phoebeliu April 1, 2013
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.customfields.view.CustomFieldParams

CustomField customField = componentManager.getCustomFieldManager().getCustomFieldObjectByName("MyCascading");
Object value = issue.getCustomFieldValue(customField);
CustomFieldParams params = (CustomFieldParams) value;
if (params != null) {
Object parent = params.getFirstValueForNullKey();
Object child = params.getFirstValueForKey("1");
}

Hi Rambanam,

I implemented the code and get the exception:

javax.script.ScriptException: org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object '{null=Settings, 1=Display}' with class 'java.util.HashMap' to class 'com.atlassian.jira.issue.customfields.view.CustomFieldParams' due to: groovy.lang.GroovyRuntimeException: Could not find matching constructor for: com.atlassian.jira.issue.customfields.view.CustomFieldParams(java.util.HashMap)

Do you have any idea?

Thanks!

RambanamP
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.
April 1, 2013

what is the jira version your using? can you try with my second answer

phoebeliu April 1, 2013

My jira version is 5.2.8

and when I use second answer, I got 4 errors:

org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:

Script386.groovy: 6: unable to resolve class Option @ line 6, column 17. HashMap hashMapEntries = (HashMap) value; ^ Script386.groovy: 6: unable to resolve class Option @ line 6, column 59. MapEntries = (HashMap) v ^ Script386.groovy: 8: unable to resolve class Option @ line 8, column 8. Option parent =hashMapEntries.get(CascadingSelectCFType.PARENT_KEY); ^ Script386.groovy: 9: unable to resolve class Option @ line 9, column 8. Option child = hashMapEntries.get(CascadingSelectCFType.CHILD_KEY); ^ 4 errors A stacktrace has been logged.

RambanamP
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.
April 1, 2013

what is the package imported for the option?

you should use this

import com.atlassian.jira.issue.customfields.option.Option;

phoebeliu April 1, 2013

it worked! Thank you!!

RambanamP
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.
April 1, 2013

glad to hear it work!!!

Note: if you find something helped to you don't forgot to voteup or accept answer

Dan Porter May 16, 2013

Rambanam,

I was trying to use this to return the value of the child option and used the following code but doesn't seem to work. I am using Groovy Script Runner Script Fields and JIRA v5.2.10. Thoughts? *Note this is just a test for something bigger I want to use the value for but I can't even return it at this point so need help to get further.

import com.atlassian.jira.issue.customfields.option.Option
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.customfields.view.CustomFieldParams
import com.atlassian.jira.issue.customfields.option.LazyLoadedOption
Object value = issue.getCustomFieldValue("Cascading Field Test")
HashMap<String, Option> hashMapEntries = (HashMap<String, Option>) value
if (hashMapEntries != null) {
Option parent = hashMapEntries.get(CascadingSelectCFType.PARENT_KEY)
Option child = hashMapEntries.get(CascadingSelectCFType.CHILD_KEY)
}
String hello = child.getValue()
return hello

RambanamP
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.
May 19, 2013

@Dan Porter,

i don't have knowledge on Groovy but the following code is using to get cascade selected field values in java

Object retVal = null;  
 if (customField.getCustomFieldType() instanceof CascadingSelectCFType) {
                    HashMap<String, Option> hashMapEntries = (HashMap<String, Option>) value;

                    if (hashMapEntries != null) {
                        Option parent =  hashMapEntries.get(CascadingSelectCFType.PARENT_KEY);
                        Option child =  hashMapEntries.get(CascadingSelectCFType.CHILD_KEY);

                        if (parent != null) {
                            if (ObjectUtils.isValueSelected(child)) {
                                retVal = child.toString();
                            } else {
                                final List<Option> childOptions = parent.getChildOptions();

                                if ((childOptions == null) || (childOptions.isEmpty())) {
                                    retVal = parent.toString();
                                }
                            }
                        }
                    }
                }

0 votes
RambanamP
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.
December 17, 2012
I hope it should help
Object value = issue.getCustomFieldValue(customField);
HashMap<String, Option> hashMapEntries = (HashMap<String, Option>) value;
if (hashMapEntries != null) {
Option parent =hashMapEntries.get(CascadingSelectCFType.PARENT_KEY);
Option child =  hashMapEntries.get(CascadingSelectCFType.CHILD_KEY);
}

ION Information Systems December 17, 2012

thanks,

I get this error:

Script32.groovy: 66: unable to resolve class Option

@ line 66, column 59.

MapEntries = (HashMap<String, Option>) A

ION Information Systems December 17, 2012

Thanks Rambanam, I have implemented your script but i get this cast exception:

javax.script.ScriptException: javax.script.ScriptException: org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object 'CustomFieldParams: ASP. Params: {null=[ASP], 1=[SG-ASP]}.' with class 'com.atlassian.jira.issue.customfields.view.CustomFieldParamsImpl' to class 'java.util.HashMap'

do you have any ideas?

thanks,

Dan Porter May 16, 2013

Rambanam,

I was trying to use this to return the value of the child option and used the following code but doesn't seem to work. I am using Groovy Script Runner Script Fields and JIRA v5.2.10. Thoughts? *Note this is just a test for something bigger I want to use the value for but I can't even return it at this point so need help to get further.

import com.atlassian.jira.issue.customfields.option.Option
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.customfields.view.CustomFieldParams
Object value = issue.getCustomFieldObjectByName("Cascading Field Test")
HashMap&lt;String, Option&gt; hashMapEntries = (HashMap&lt;String, Option&gt;) value
if (hashMapEntries != null) {
Option parent = hashMapEntries.get(CascadingSelectCFType.PARENT_KEY)
Option child = hashMapEntries.get(CascadingSelectCFType.CHILD_KEY)
}
String hello = child.toString()
return hello

Dan Porter May 16, 2013

Sorry that last line should read "String hello = child.getValue()

Bhavana S July 20, 2016
Does the below code  work for now
import com.atlassian.jira.issue.customfields.option.Option
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.customfields.view.CustomFieldParams
Object value = issue.getCustomFieldObjectByName("Cascading Field Test")
HashMap<String, Option> hashMapEntries = (HashMap<String, Option>) value
if (hashMapEntries != null) {
Option parent = hashMapEntries.get(CascadingSelectCFType.PARENT_KEY)
Option child = hashMapEntries.get(CascadingSelectCFType.CHILD_KEY)
}
String hello = child.toString()
return hello
Like Rafal Sojka likes this
Bhavana S July 20, 2016

I have problem with

Object value = issue.getCustomFieldObjectByName("Cascading Field Test")

0 votes
Mizan
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.
December 17, 2012

you can refer this post

TAGS
AUG Leaders

Atlassian Community Events