I found solution here, it tells me use Option object instead of a string value. see https://answers.atlassian.com/questions/10431/how-do-i-update-the-value-of-a-selectcftype-field
But when I tried
Options options = this.getOptions(issueObject, cf);
Option newVal = null;
for(Object obj : options){
Option opt = (Option) obj;
if(opt.getValue().equals(strVal){
newVal = opt;
break;
}
}
issueObject.setCustomFieldValue(envrionmentType, newVal);
or
ModifiedValue modifiedValue = new ModifiedValue(issueObject.getCustomFieldValue(cf), newVal);
IssueChangeHolder changeHolder = new DefaultIssueChangeHolder();
cf.updateValue(null, issueObject, modifiedValue, changeHolder);
Neither of these two works.
Anybody can help me?
I got the answer from atlassian support.
thanks them and specially thanks for @Dieter' help on this.
I pasted the code below to save time for anyone encountering similar problem.
FieldConfig fieldConfig = timezoneCustomField.getRelevantConfig(issue);
final Options options = optionsManager.getOptions(fieldConfig);
com.atlassian.jira.issue.customfields.option.Option tzOption = options.getOptionForValue(newTimezoneStr, null);
if (tzOption == null) {
log.error("Can't set custom field value to " + newTimezoneStr + " because it doesn't correspond to a valid custom field option.");
}
else {
issue.setCustomFieldValue(timezoneCustomField, tzOption);
if(log.isDebugEnabled()){
log.debug("Update TZ for issue '"+issue.getKey()+"' to '"+newTimezoneStr+"'");
}
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.
Before you call cf.updateValue you must set the change field in the issue as you already noticed:
issueObject.setCustomFieldValue(envrionmentType, newVal);
This must be done before cf.updateValue
Then i noticed you do not pass a fieldLayoutItem (1st argument) to cf.updateValue. I guess this doesn't work
The first argument can be retrieved from the following function
public static FieldLayoutItem getFieldLayoutItem(Issue issue, Field field) throws FieldLayoutStorageException {
final FieldLayoutManager fieldLayoutManager = ComponentManager.getInstance().getFieldLayoutManager();
FieldLayout layout = fieldLayoutManager.getFieldLayout(
issue.getProjectObject().getGenericValue(),
issue.getIssueTypeObject().getId()
);
if (layout.getId() == null) {
layout = fieldLayoutManager.getEditableDefaultFieldLayout();
}
return layout.getFieldLayoutItem(field.getId());
}
So to put it together
FieldLayoutItem flo = getFieldLayoutItem (issueObject, cf); ModifiedValue modifiedValue = new ModifiedValue(issueObject.getCustomFieldValue(cf), newVal); issueObject.setCustomFieldValue(cf, newVal); IssueChangeHolder changeHolder = new DefaultIssueChangeHolder(); cf.updateValue(flo, issueObject, modifiedValue, changeHolder);
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.