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

Deleted user July 26, 2017

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
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.
July 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

 

 

Deleted user July 27, 2017

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?

Joe Jadamec January 22, 2018
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
TAGS
AUG Leaders

Atlassian Community Events