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

Increment a numeric custom field post function Edited

Jonathan Down Dec 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 [cPrime] Community Leader Dec 12, 2017

Hello,

Could you provide the script which is not working?

Jonathan Down Dec 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 [cPrime] Community Leader Dec 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 Dec 12, 2017 • edited

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 [cPrime] Community Leader Dec 12, 2017

You define it "CR Ready"

Caroline Courchesne Feb 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
This widget could not be displayed.
This widget could not be displayed.
Community showcase
Published in Marketplace Apps & Integrations

Webinar alert! An IT Admin's Guide to Securing Collaboration at Scale

Hello, Atlassian Community! My name is Dave Meyer and I'm a Principal Product Manager at Atlassian. I wanted to give this community a heads up about an upcoming Webinar that might be of interest...

186 views 2 7
Read article

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