Just a heads up: On March 24, 2025, starting at 4:30pm CDT / 21:30 UTC, the site will be undergoing scheduled maintenance for a few hours. During this time, the site might be unavailable for a short while. Thanks for your patience.
×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.
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!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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);
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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);
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Sorry for all the edits, the format was all messed up.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Apologies for the late reply, but unfortunately that didn't work either.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
@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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Eric! By any chance have you solved this problem? If so can you please post it on here?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Online forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.