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 {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

This widget could not be displayed.
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 :

getCustomFieldObjectByName(customFieldName)

has now been deprecated.

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

This widget could not be displayed.

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

 

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

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

This widget could not be displayed.

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

This widget could not be displayed.

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

This widget could not be displayed.

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

Suggest an answer

Log in or Sign up to answer
Atlassian Summit 2018

Meet the community IRL

Atlassian Summit is an excellent opportunity for in-person support, training, and networking.

Learn more
Community showcase
Posted Wednesday in New to Jira

Are you planning to trial, or are currently trialling Jira Software? - We want to talk to you!

Hello! I'm Rayen, a product manager at Atlassian. My team and I are working hard to improve the trial experience for Jira Software Cloud. We are interested in   talking to 20 people planning t...

105 views 2 0
Join discussion

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