Post Function Increment Counter

Richard Duffy November 17, 2019

Hi

Looking for some help with a post function script:

Tools: Scriptunner JSU

Version: Jira 8.4.2

Problem statement: I want to enable a counter based on a specific workflow transition, eg - When the reopen transition is fired from the status fixed - add 1 to the counter field

Method:  (Custom script post function)

Field created: Scripted field (customfield_13502) - number searcher

Code:

import com.atlassian.jira.ComponentAccessor
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.customfields.CustomFieldType
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.issue.util.IssueChangeHolder
import com.atlassian.jira.issue.IssueManager

IssueManager issueManager = ComponentAccessor.getIssueManager()
CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()
CustomField cf = customFieldManager.getCustomFieldObject("customfield_13502")
Double val = (issue.getCustomFieldValue(cf) as Double)
if ( val == null)
val = 1
else
val = val + 1
IssueChangeHolder changeHolder = new DefaultIssueChangeHolder();
cf.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(cf),val), changeHolder)

 

Thanks in advance

Rich

1 answer

1 accepted

0 votes
Answer accepted
Leo
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
November 17, 2019

Hi @Richard Duffy

maybe you can try this , since there is already re-index function in post-function order need not to use changeholder. you can use that if you are working with listerners 

def val = issue.getCustomFieldValue(cf) as Integer

if (val)
val = val + 1
else
val = 1
issue.setCustomFieldValue(cf, val)

 

BR,

Leo

Richard Duffy November 17, 2019

Thanks for quick reply Leo

IssueManager issueManager = ComponentAccessor.getIssueManager()
CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()
CustomField cf = customFieldManager.getCustomFieldObject("customfield_13502")
def val = issue.getCustomFieldValue(cf) as Integer

if (val)
val = val + 1
else
val = 1
issue.setCustomFieldValue(cf, val)

I have edited the script to the above. Still no luck

Is my Custom field correct using "Scripted Field" with number template?

Leo
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
November 17, 2019

Are you trying with scripted field?

if so you need to place script in it's place. but I don't think for your requirement scripted field is required as you want to update this field only on particular workflow transition

my bad I missed to notice scripted field you mentioned :( 

I would suggest you to create Number Field(custom field not scripted field) and use this post-function script

 

BR,

Leo

Richard Duffy November 17, 2019

Yes correct regards requirement. I have changed the field to Number field.

Getting workflow error now when executing the transition 

Error occurred while creating issue. This could be due to a plugin being incompatible with this version of JIRA. For more details please consult the logs, and see: http://confluence.atlassian.com/x/3McB java.lang.Integer cannot be cast to java.lang.Double

It seems that you have tried to perform an illegal workflow operation.

If you think this message is wrong, please contact your Jira administrators.

Leo
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
November 17, 2019

ah gotcha, seems like I was wrong 

you must go with Double instead of Integer as you were before 

Double val = issue.getCustomFieldValue(cf) as Double
Richard Duffy November 17, 2019

Great Leo, its working now. Thanks for your guidance

Aradhya Kahate September 11, 2023

Hi Leo,

For me it is only incrementing till value2 not more than that.

Can you suggest Something?

Suggest an answer

Log in or Sign up to answer