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

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 { == "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.

4 answers

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

This worked perfectly - thanks Daniel!

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?


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


Note that the method :


has now been deprecated.

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

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 { == "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);


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

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

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

@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.

Suggest an answer

Log in or Join to answer

Stay in touch

Be the first to know what's trending on Atlassian Community