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,
Community moderators have prevented the ability to post new answers.
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"); }
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
if it works accept as a correct answer! :)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
what is the jira version your using? can you try with my second answer
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
what is the package imported for the option?
you should use this
import com.atlassian.jira.issue.customfields.option.Option;
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
glad to hear it work!!!
Note: if you find something helped to you don't forgot to voteup or accept answer
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
@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(); } } } } }
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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); }
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
thanks,
I get this error:
Script32.groovy: 66: unable to resolve class Option
@ line 66, column 59.
MapEntries = (HashMap<String, Option>) A
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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,
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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<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
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.
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
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 must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.