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 votes
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 Sign up to answer
Community showcase
Published Nov 27, 2018 in Portfolio for Jira

Introducing a new planning experience in Portfolio for Jira (Server/DC)

In the past, Portfolio for Jira required a high degree of detail–foresight that was unrealistic for many businesses to   have–in   order to produce a reliable long-term roadmap. We're tur...

2,950 views 19 22
Read article

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