How to copy all child options of cascading field to another text field

What I have here are :

Cascading field :

ParentOption A

Child Options 1, 2, 3

ParentOption B

Child Options 5,6, 4

 

In the ticket ParentOption B is selected.

I want to copy all childoptions 5, 6,4 for parentOption B to a free text field.


Here is my code:

{code}

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder;
import com.atlassian.jira.issue.ModifiedValue;
import com.atlassian.jira.issue.customfields.manager.OptionsManager
import com.atlassian.jira.issue.customfields.option.Option
CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()
OptionsManager optionsManager = ComponentAccessor.getOptionsManager();

CustomField cf = customFieldManager.getCustomFieldObject("customfield_15400");
def customfield2 = customFieldManager.getCustomFieldObject('customfield_10401');
def changeHolder = new DefaultIssueChangeHolder();

Map cfVal = issue.getCustomFieldValue(cf) as Map

if (cfVal) {

String stParentOption = cfVal.get(null);
String stChildOption = cfVal.get("1");
//log.debug ("\n Incident: stParentOption = " + stParentOption);
//log.debug ("\n Incident: stChildOption = " + stChildOption);

 

Option parentOptionObj = optionsManager.getOptions(cf.getRelevantConfig(issue)).getOptionForValue(stParentOption,null)
log.debug ("\n Target Parent Classification = " + parentOptionObj);

Option childOptionObj = optionsManager.getOptions(cf.getRelevantConfig(issue)).getOptionForValue(stChildOption,parentOptionObj.optionId)
log.debug ("\n Target Classification child ("+ parentOptionObj + ") = " + childOptionObj);
customfield2.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(customfield2), childOptionObj), changeHolder);
}
else {
log.info("Custom field not present on this issue")
}

{code}

It prompt me error 

java.lang.ClassCastException: com.atlassian.jira.issue.customfields.option.LazyLoadedOption cannot be cast to java.lang.String

 How do I convert Options to string or how do I copied all the child option to a free text field?

I'm new to groovy script, any helps will be much appreciated.

1 answer

0 vote
Gaston Valente Community Champion Jul 27, 2017

Hi, the option interface has a method getValue that returns the string you need i think:

https://docs.atlassian.com/jira/server/com/atlassian/jira/issue/customfields/option/Option.html

 

 

I have added toString() function to this line and it print only the selected child option.

ModifiedValue(issue.getCustomFieldValue(customfield2), childOptionObj.toString()), changeHolder);

Yes I had used getValue  and it only return value of one option.
How do I get all options for the child field?

CustomFieldManager cFM = ComponentAccessor.getCustomFieldManager();
CustomField cf = cFM.getCustomFieldObject(fieldId); //your custom field id (Long)
OptionsManager optionsManager = ComponentAccessor.getOptionsManager();
com.atlassian.jira.issue.context.IssueContextImpl issueContext = new com.atlassian.jira.issue.context.IssueContextImpl(issue.getProjectId(), issue.getIssueTypeId());
FieldConfig fieldConfig = cf.getRelevantConfig(issueContext);
Options options = optionsManager.getOptions(fieldConfig);
for (Option option in options.getRootOptions()){
String parentOptionValue = option.getValue();
//do something with parentOptionValue
List<Option> childOptions = option.getChildOptions();
for(Option childOption in childOptions){
String childOptionValue = childOption.getValue();
//do something with childOptionValue
}
}

Suggest an answer

Log in or Join to answer
Community showcase
Sarah Schuster
Posted Jan 29, 2018 in Jira

What are common themes you've seen across successful & failed Jira Software implementations?

Hey everyone! My name is Sarah Schuster, and I'm a Customer Success Manager in Atlassian specializing in Jira Software Cloud. Over the next few weeks I will be posting discussion topics (8 total) to ...

3,307 views 14 20
Join discussion

Atlassian User Groups

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

Find a group

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

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you
Atlassian Team Tour

Join us on the Team Tour

We're bringing product updates and pro tips on teamwork to ten cities around the world.

Save your spot