Decrement Custom Field Value

AMOL October 28, 2017

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

0 votes
Answer accepted
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.
October 29, 2017

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. 

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.
October 29, 2017

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()

 

AMOL October 30, 2017

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

0 votes
Answer accepted
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.
October 29, 2017

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. 

AMOL October 29, 2017

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
Answer accepted
Tayyab Bashir
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.
October 28, 2017

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.

AMOL October 29, 2017

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

AMOL October 29, 2017

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

Nic Brough -Adaptavist-
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
October 29, 2017

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

AMOL October 30, 2017

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
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.
October 28, 2017

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. 

AMOL October 29, 2017

ScriptRunner.

Suggest an answer

Log in or Sign up to answer