Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Post Function Increment Counter

Richard Duffy
Contributor
November 17, 2019 edited

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 Champion
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
Contributor
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 Champion
November 17, 2019 edited

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
Contributor
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 Champion
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
Contributor
November 17, 2019

Great Leo, its working now. Thanks for your guidance

Aradhya Kahate
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
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
TAGS
atlassian, team '25 europe, atlassian event, barcelona 2025, jira, confluence, atlassian intelligence, rovo, ai-powered collaboration, developer tools, agile teams, digital transformation, teamwork solutions, atlassian conference, product announcements

🌆 Team '25 Europe registration is now open!

Join the largest European gathering of the Atlassian Community and reimagine what’s possible when great teams and transformative technology come together. Plus, grab your Super Fan ticket now and save over €1,000 on your pass before prices rise on 3 June.

Register now
AUG Leaders

Atlassian Community Events