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.
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?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
}
}
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.