Decrement Custom Field Value

I have a Scripted custom field called FixVersion_Changed

The value of the field is updated as it captures value for number of changes made to the FixVersions field.

I would like to decrement the value of this field under certain conditions using a Scripted Post Function.

I am not a programmer and looking for a code that I can use to modify the value of this field.

4 answers

3 accepted

You need to add the post-function to a workflow step. 
You could add a code something like this to decrement the value:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder

// Replace 12345 by the ID of your customfield
def customField = ComponentAccessor.customFieldManager.getCustomFieldObject(12345)

def fieldValue = issue.getCustomFieldValue(customField)
def newValue

// Add your conditions here
if (true){
newValue = fieldValue -1
}

def holder = new DefaultIssueChangeHolder()
customField.updateValue(null, issue, new ModifiedValue(fieldValue, newValue), holder)

You can modify the code, adjust it to your conditions to apply decrement.

Thank you, Tayyab. I am going to try this out. Will let you know here.

Getting the below error...

Also if I specify the fieldID, I get few more errors.. Again I am not a Java or Groovy programmer to decipher

Capture.PNG

Use spaces in your code.  "fieldvalue-1" is a single object that you have not defined, not a calculation.  Try "fieldvalue - 1"

Thank you Nic and Tayyab. Alexey's solution below worked for me. I do have a use case for the code mentioned above as well. Will test it soon.

0 votes
Accepted answer

if you use scriptrunner scripted field then you will not be able to set a value for the field in a post-function. Scripted fields work differently. The value for the scripted field is calculated on showing the field on the screen. That is why you have to change the code for the field. I guess there is an increment somewhere in the code of the scripted filed. You need to add a condition which will skip the increment on your condition. If you produce the current code of the scripted filed I can give you more details. 

Hi Alex,

That makes sense..

The Scripted field name is FixVersion_Changed. Below is the code of the scripted field.

***

import com.atlassian.jira.component.ComponentAccessor
ComponentAccessor.changeHistoryManager?.getChangeItemsForField(issue, "Fix Version").size() as int

***

I would like to not increment the value of this field if I transition the workflow to Backlog and Deferred. This is because I clear the FixVersion field when I transition to Backlog or Deferred as a post-function. The issue could come back in the funnel at a later date and a new FixVersion could be assigned.

0 votes
Accepted answer

you need to replace

ComponentAccessor.changeHistoryManager?.getChangeItemsForField(issue, "Fix Version").size() as int

with something like this

ComponentAccessor.changeHistoryManager?.getChangeItemsForField(issue, "Fix Version").findAll{ !it.getToString().isEmpty() }.size() as int

Unfortunately I can not check the code because I have no access to Jira right now. But the idea is that you have to count all changes of Fix Version field except when it was set to Null. I am not sure what getToString returns if value were set to null but maybe it will work. 

Hello,

Use this code. It worked for me.

import com.atlassian.jira.component.ComponentAccessor
ComponentAccessor.changeHistoryManager?.getChangeItemsForField(issue, "Fix Version").findAll{ it.getToString() != null}.size()

 

This is working exactly as needed. Thank you, Alexey!!

0 votes

Hello, 

What plugin do you use for the scripted field?  Scriptrunner? If you are not sure then kindly produce the current code of the mentioned scripted filed. 

ScriptRunner.

Suggest an answer

Log in or Sign up to answer
Community showcase
Published Nov 27, 2018 in Portfolio for Jira

Introducing a new planning experience in Portfolio for Jira (Server/DC)

In the past, Portfolio for Jira required a high degree of detail–foresight that was unrealistic for many businesses to   have–in   order to produce a reliable long-term roadmap. We're tur...

2,676 views 18 21
Read article

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