Update numeric custom field value using custom groovy script post function to save to database

I am needing to write a custom groovy script post function to update and store a custom field value to the database. (I originally used Scripted Fields successfully, but need  a way to access the values for custom emails/etc).  I am fairly new to this so am having trouble getting values to update on my ticket.  Below is the code I am using where my custom field is a numeric field.


Any ideas on what I'm doing wrong?

import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.util.IssueChangeHolder
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder

ComponentManager componentManager = ComponentManager.getInstance();
CustomFieldManager customFieldManager = componentManager.getCustomFieldManager();
CustomField travelcost=customFieldManager.getCustomFieldObject(“customfield_11705”);
def valuetravelcost = issue.getCustomFieldValue(travelcost)
def changeHolder=new DefaultIssueChangeHolder();
travelcost.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(travelcost),1), changeHolder);

1 answer

This code should be enough, anyway the usage depends on the place.

If your code is placed in the postfunction, there is just enough to use issue.setCustomFieldValue(travelcost, value), when your postfunction  is placed before built-in function Update change history for an issue and store the issue in the database (beware that Create transition is slightly different case).

Otherwise - your maybe just not reindexed.

The above code is placed on a non-Create transition. I had the code above in a post function before the Update Change History for an Issue..." post function. There is a Re-index an issue to keep indexes in sync with the database post function directly following the update post function. It still doesn't work. I also tried issue.setCustomFieldValue(travelcost,1) instead of the updateValue line. That too isn't populating the value. I'm out of ideas.

Suggest an answer

Log in or Sign up to answer
Community showcase
Published Feb 07, 2019 in Marketplace Apps

A Timeless Love Story

It started as any story starts, on a normal, rainy day.   Admin meets App, and her name was Klok2, and like any first relationship we were both trying to make it work but neither one knew what...

441 views 8 26
Read article

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