Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

How do I update a custom field using a post function on a workflow?

Michelle Genrich
Contributor
November 5, 2018

I added a custom field that I want to increment each time the workflow transitions to the status.  I read the other posts on this, but didn't find the answer I needed. This is what I have done:

  • Created a custom field called Rework. The field type is number.
  • Added the custom field to the pages for my project.
  • Confirmed I could see the field and update it on create and edit of an issue.
  • Added a post function to increment the field Rework.

When I transition the issue I get an error that the custom field cannot be updated. I have checked the permissions and screens, and it's there. What else can I do to fix this?

 

This is the code for the post function:

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

 

String customFieldName = "Counter";
DefaultIssueChangeHolder changeHolder = new DefaultIssueChangeHolder();
CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager();
CustomField cf = customFieldManager.getCustomFieldObject(10047);

Double currValue = (Double)cf.getValue(issue);
Double newValue = currValue+1;

cf.updateValue(null, issue, new ModifiedValue(currValue,newValue), changeHolder);

 

I also tried:

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

//Update "Churn Number" to the name of your custom field.
String customFieldName = "Rework";
DefaultIssueChangeHolder changeHolder = new DefaultIssueChangeHolder();
CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager();


//Update "Churn Number" to the name of your custom field.
CustomField cf = customFieldManager.getCustomFieldObject(10047);

//NOTES: If the current value is 0 use 0 not null. If it is a real number use the real number.
def currValue = (Double)cf.getValue(issue) ?: 0
def newValue = currValue+1;
cf.updateValue(null, issue, new ModifiedValue(currValue,newValue), changeHolder);

 

This is the error:

Unable to update custom field customfield_10047 - Rework in issue [TSP-11].

What did I miss?

0 answers

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events