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

Melissa Bay February 27, 2015

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

0 votes
Jozef Kotlár
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.
March 1, 2015

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.

Melissa Bay March 3, 2015

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
TAGS
AUG Leaders

Atlassian Community Events