Increment a numeric custom field post function

Jonathan Down December 12, 2017

Hi I'd like to increment a custom field when certain transitions take place. The default value is 0 and should count upwards by 1 each time a post function is run. I've searched around and found a few different answers but when I put any of them into the Code box all the import statements show "unable to resolve X".

 

Does anyone have a scriptrunner script I can use? Thanks!

1 answer

0 votes
Alexey Matveev
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.
December 12, 2017

Hello,

Could you provide the script which is not working?

Jonathan Down December 12, 2017
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;

Long customFieldId = 10065;
DefaultIssueChangeHolder changeHolder = new DefaultIssueChangeHolder();
CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager();
CustomField cf = customFieldManager.getCustomFieldObjectByName(customFieldId);

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

cf.updateValue(null, issue, new ModifiedValue(currValue,newValue), changeHolder);

I found it here:

https://community.atlassian.com/t5/Jira-questions/How-can-I-increment-a-custom-field-value-in-a-post-function/qaq-p/74690

When I paste that into the code box all the import statements show an X and can't resolve Name.

Alexey Matveev
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.
December 12, 2017

Change the script to this one and replace customFieldName with the name of your 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;


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);
Jonathan Down December 12, 2017

How do I define "customFieldName"? The page I linked says to use a Long but your response suggests a String. If the field has ID 10065 and is named "CR Ready" what should the parameter value be?

Alexey Matveev
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.
December 12, 2017

You define it "CR Ready"

Caroline_Courchesne February 13, 2018

Hello, I'm a beginner with the scripts and I want to do the same thing as Jonathan but for how many times an issue has been reopened. So I entered this script in the Reopen transition and I created a numeric custom field with a default value of 0.

Each time I test it with a new request, I get this error : 

java.lang.NullPointerException: Cannot invoke method getValue() on null object

I'm pretty sure it's really easy to fix but everything is new to me so... :|

What should I do?

Thanks!

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events