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
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?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Great Leo, its working now. Thanks for your guidance
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Leo,
For me it is only incrementing till value2 not more than that.
Can you suggest Something?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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 nowOnline forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.