Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

Post Function Increment Counter 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 Leader Nov 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

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 Nov 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

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 Nov 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

Great Leo, its working now. Thanks for your guidance

Suggest an answer

Log in or Sign up to answer
TAGS

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you