Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Next challenges

Recent achievements

  • Global
  • Personal

Recognition

  • Give kudos
  • Received
  • Given

Leaderboard

  • Global

Trophy case

Kudos (beta program)

Kudos logo

You've been invited into the Kudos (beta program) private group. Chat with others in the program, or give feedback to Atlassian.

View group

It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

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

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.

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

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

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.

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 Sign up to answer
TAGS

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you