how to set a 'Select List' type custom field programably

Deleted user November 24, 2011

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?

4 answers

2 votes
Deleted user November 28, 2011

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+"'");
}

Greg Felice June 19, 2012

What if you want to set a select field to null?

leejimin1006 August 28, 2012

Thank you very much.

Luke Galardi January 23, 2019

Thank You.  

0 votes
Dieter
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.
November 27, 2011

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);


0 votes
Deleted user November 24, 2011

Hi Dieter,

Actually I have a null check before use the newVal in code, I did not paste here.

Is the way I use right?

Or can you help show how to use Option object solve this problem? thanks a lot!

0 votes
Dieter
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.
November 24, 2011
Did you make sure newVal is not null? Are there any messages in atlassian-jira.log?

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events