How to update the Custom field value(select option) in post-function?

  1.  Create a custom field "abc" and its selection options are "aa","bb"
  2. In the post-function, I added the Custom script post-function. How to achieve the custom field "abc" equal to "aa" or "bb" and update it on the issue in groovy script?
  3. If I run the following code in JIRA, I got the following error: java.util.String cannot be cast to com.atlassian.jira.issue.customfields.option.Option.  What should I do to achieve this feature? thanks a lot.




log = Category.getInstance("com.onresolve.jira.groovy.AutoCloseChildIssues");
def ComponentManager compoentManager = ComponentManager.getInstance();
def CustomFieldManager customFieldManager = compoentManager.getCustomFieldManager();
def CustomField deliverCycleField = customFieldManager.getCustomFieldObjectByName("customeTime");
def deliverCycleValue = issue.getCustomFieldValue(deliverCycleField);
def CustomField deliverCycleCountField = customFieldManager.getCustomFieldObjectByName("abc");

if (deliverCycleValue <= 16) {
issue.setCustomFieldValue(deliverCycleCountField, "aa");

} else {
issue.setCustomFieldValue(deliverCycleCountField, "bb");
log.error("-------------------------------------------5" + "deliverCycleValue=" + deliverCycleValue);
}
def FieldLayoutItem fieldLayoutItem = compoentManager.getFieldLayoutManager().getFieldLayout(issue).getFieldLayoutItem(deliverCycleCountField);
DefaultIssueChangeHolder defaultIssueChangeHolder = new DefaultIssueChangeHolder();
def modifiedFields = issue.getModifiedFields();
final ModifiedValue modifiedValue = (ModifiedValue) modifiedFields.get(deliverCycleCountField.getId());
deliverCycleCountField.updateValue(fieldLayoutItem, issue, modifiedValue, defaultIssueChangeHolder);

 

 

 


1 answer

issue.getCustomFieldValue(deliverCycleField) method will return Option object so you need use condition something like, verfy this api before using 
if (deliverCycleValue <= deliverCycleValue .name)

You need to pass Option object to the following method instead of 'aa'

issue.setCustomFieldValue(deliverCycleCountField, "aa");

check the following post to know how to get the option object

https://answers.atlassian.com/questions/18840

Suggest an answer

Log in or Join to answer
Community showcase
Sarah Schuster
Posted Jan 29, 2018 in Jira

What are common themes you've seen across successful & failed Jira Software implementations?

Hey everyone! My name is Sarah Schuster, and I'm a Customer Success Manager in Atlassian specializing in Jira Software Cloud. Over the next few weeks I will be posting discussion topics (8 total) to ...

3,308 views 14 20
Join discussion

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
Atlassian Team Tour

Join us on the Team Tour

We're bringing product updates and pro tips on teamwork to ten cities around the world.

Save your spot