How can I increment a custom field value in a post function?

Eric Kaiser October 9, 2014

Hi there,

I have a numerical custom field, and I'm trying to increment its value through a workflow post function using the Script Runner plugin.

I know this has been asked and answered numerous times before, but I have yet to find a piece of code that actually works. I know very little about Groovy, so I've basically resorted to copy and paste.

This is what I currently have, which does nothing at all:

def tgtField = customFieldManager.getCustomFieldObjects(issue).find {it.name == "Iterations"} // <-- that's what the field is called
def changeHolder = new DefaultIssueChangeHolder();
tgtField.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(tgtField), issue.getCustomFieldValue(tgtField) + 1),changeHolder);

What am I doing wrong, and more importantly, what should I do instead? Any help is greatly appreciated.

5 answers

7 votes
Daniel Micallef January 12, 2016
I have been able to solve this post function with the following script. The aim here is to set a postfunction able to increment a custom field.

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.getCustomFieldObjectByName(customFieldName);

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

cf.updateValue(null, issue, new ModifiedValue(currValue,newValue), changeHolder);
Steve Quintana September 21, 2016

This worked perfectly - thanks Daniel!

Joe Miller November 7, 2016

So I have a status, In Progress which moves to Ready To Test.  I want to track when a ticket goes back from Ready To Test to In Progress.  How would I modify the above script to allow for that?

 

Daniel Micallef November 8, 2016

Add the above script as a post function in the transition from 'Ready To Test' to 'In Progress'.

 

Note that the method :

getCustomFieldObjectByName(customFieldName)

has now been deprecated.

Use getCustomFieldObject(Long id) instead, and supply the Customfield Id instead of the name.

2 votes
Michael Swansegar January 18, 2018

Here is an updated example that also works for null values and has code comments!

 

 

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;

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


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

//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);

Dave Furlani February 17, 2019

Replace this line in the middle:

CustomField cf = customFieldManager.getCustomFieldObjectByName("Churn Number");

with this line and remove the comment above it

CustomField cf = customFieldManager.getCustomFieldObjectByName(customFieldName);

And that script is perfect.  I can confirm it works in 7.11

1 vote
Ramiro Pointis
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.
October 9, 2014

Maybe it can't be read inside the function:

tgtField.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(tgtField), issue.getCustomFieldValue(tgtField) + 1),changeHolder);

Have you tried changing the value before like this:

def tgtField = customFieldManager.getCustomFieldObjects(issue).find {it.name == "Iterations"}
def changeHolder = new DefaultIssueChangeHolder();
def old_value = issue.getCustomFieldValue(tgtField)
def new_value = old_value + 1
tgtField.updateValue(null, issue, new ModifiedValue(old_value, new_value),changeHolder);

 

Ramiro Pointis
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.
October 9, 2014

Sorry for all the edits, the format was all messed up.

Eric Kaiser January 2, 2015

Apologies for the late reply, but unfortunately that didn't work either.

0 votes
Eric Kaiser January 3, 2016

@Daniel Micallef Hi Daniel - sorry to say, but I wasn't able to get it working. I eventually gave up on it because it wasn't terribly important.

0 votes
Daniel Micallef December 17, 2015

Hi Eric! By any chance have you solved this problem? If so can you please post it on here?

Suggest an answer

Log in or Sign up to answer