Automatically Increment Custom Field on Transition

Greetings.  I have looked around and found pieces of code but no real solution.  I don't care if it is scriptrunner or not but I am open to suggestions.

In the workflow I want to add a post function to a few of the transitions that go to a number custom field and increase it by 1.  So if it was blank before it becomes 1.  if it is 3 it becomes 4.

 

This seems rather simple but I am hitting my head on the wall of what to do.  I am on JIRA 7.6 and ScriptRunner 5.5.2 .  Again, open to suggestions.

2 answers

1 accepted

That was outdated, this is actual working code with 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);

J D I'm New Here Feb 01, 2018

Do you have a script that works on Jira Cloud? When I put that code in it shows errors for all the import statements and fails when the transition occurs.

Screen Shot 2018-02-01 at 10.29.18 AM.png

Figured it out in case anyone else has the same issue. This is the script:

def currValue = (Double)issueInput.fields.customfield_10065 ?: 0 issueInput.fields.customfield_10065 = currValue + 1.0

You can get the field ID from the URL when you edit the field.

Suggest an answer

Log in or Join to answer
Community showcase
Sarah Schuster
Posted Jan 29, 2018 in Jira

What are common themes you've seen across successful & failed Jira Software implementations?

Hey everyone! My name is Sarah Schuster, and I'm a Customer Success Manager in Atlassian specializing in Jira Software Cloud. Over the next few weeks I will be posting discussion topics (8 total) to ...

3,277 views 14 20
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
Atlassian Team Tour

Join us on the Team Tour

We're bringing product updates and pro tips on teamwork to ten cities around the world.

Save your spot