Set custom field (select list) value

Hello!

I've got a customfield_16301 (select list type).

It's got three options: High, Mid, Low.

How do I set an option in customfield_16301 using groovy?

ComponentManager componentManager = ComponentManager.getInstance()

CustomFieldManager customFieldManager = componentManager.getCustomFieldManager()

CustomField customField = customFieldManager.getCustomFieldObject("customfield_16301");

issue.setCustomFieldValue(customField, "High")

Throws NullPtr expections!

Thank you.

1 answer

1 accepted

2 votes
Answer accepted

Hi et,

What is the value of your cstomField object?

What do you mean?

How do I check that?

Hi e t,

This thread discusses a similar problem:

https://answers.atlassian.com/questions/189262/how-do-i-programmatically-set-values-for-custom-fields-of-type-select-when-creating-an-issue

The solution proposed here should work for all list select type fields.

Try logging the customField variable in your script. What is its value?

Your script should look something like:

CustomField customField = customFieldManager.getCustomFieldObject("customfield_16301");
 
Options options = WebAppsCf.getOptions(null, customField.getRelevantConfig(issue), null);   
 
Option newOption = options.getOptionById("high option"); // Check your option id
 
ModifiedValue mVal = new ModifiedValue(issue.getCustomFieldValue(customField), newOption );
 
customField.updateValue(null, issue, mVal, new DefaultIssueChangeHolder());

Oh, thanks!

What's "WebAppsCf" ?

// Check your option id - I guess it's 15602

Indeed, you don't need the WebAppCf class. I modified the previous script:

CustomField customField = customFieldManager.getCustomFieldObject("customfield_16301");

OptionsManager optManager = ComponentAccessor.getOptionsManager(); 
Options options = optManager.getOptions(customField.getRelevantConfig(issue), null));   
 
Option newOption = options.getOptionById("high option"); // Check your option id
 
ModifiedValue mVal = new ModifiedValue(issue.getCustomFieldValue(customField), newOption );
 
customField.updateValue(null, issue, mVal, new DefaultIssueChangeHolder());

Thank you! It worked!

Hi e t,

Can you please mark the answer as accepted? :)

Glad I could help!

BTW: there is a small typo: Options options = optManager.getOptions(customField.getRelevantConfig(issue), null)); should be Options options = optManager.getOptions(customField.getRelevantConfig(issue))

Suggest an answer

Log in or Sign up to answer
Community showcase
Posted 12 hours ago in United States

Atlassian acquires AgileCraft

         Good Day, Bad bad traffic, not sure why!!!! 1/2 hour commute took me 2 hours today 🤯 What helped me is that I kept browsing LinkedIn until...

46 views 4 0
View post

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