Updating a custom field by +1 everytime issue is reopened

Hi,

I am using behaviour plugin to update value of a custom field everytime a issue is reopened.. I am using the below code but no luck!!..Can anyone pls help

<config use-validator-plugin="false" name="Reopen Counter behaviour" description="" guideWorkflow="Test">

  <field id="customfield_12051" required="null" readonly="false" hidden="false" validator-class="" validator-script="import com.onresolve.jira.groovy.user.FormField

FormField formUserField = getFieldByName("ReopenCounter")

formUserField.setFormValue(Integer.valueOf(formUserField.getFormValue()) + 1)
" validator-method="">

    <when action="3"/>

  </field>

</config>

I am new to using this plugin so pls enlighten on mistakes if any or if I am missing something!!
Thanks

3 answers

1 accepted

This widget could not be displayed.

Hey,

I know that this isn't really answering your question but I think you're better off using for example the script runner to inject a script which is increasing the field value by one as a post-function.

https://marketplace.atlassian.com/plugins/com.onresolve.jira.groovy.groovyrunner

You don't need to update the field in real-time and that's the benefit of the behaviours plugin...

You could also think of the Misc Workflow Extensions Addon:

https://marketplace.atlassian.com/plugins/com.innovalog.jmwe.jira-misc-workflow-extensions

I know that it's not free anymore but if offers all kinds of useful post-functions such as "increasing the value of a field":

https://innovalog.atlassian.net/wiki/display/JMWE/Post-functions#Post-functions-4-IncreasevalueoffieldFunction

Cheers

Christian

Exactly what Christian said. If you don't need to use the behaviours plugin and you can do everything server-side, then do it. Use behaviours when you want dynamic form behaviour, or you want the user to be able to override the suggestion.

Thanks Christain, I used script runner plugin for this and it worked

Hi When you are updating the custom field is it for that particular ticket or for ever for that project.

 

I even need to decrease the custom field value but next when anyone logs in he should see the decreased value. How can I achieve it?

Projects don't have fields, so it's for the issue.

When a value is changed via a script or behaviour, you will see it the next time you land on or refresh a page

Hi Nic,

 

Can you guide me how can I achieve it. I was using javascript to add a value in the custom field but when I refresh the page the custom field does not have that value. It contains only for that issue. How can I change the value in the custom field so that it exists even after refresh. Kindly guide me to achieve it.

I wouldn't do it with Javascript, that way madness lies.  Use a proper field and a bit of code to change the value.

What do you have in the way of scripting add-ons?

This widget could not be displayed.
Issue issue  = issue

String field = "customfield_12250"
CustomFieldManager customFieldManager = componentManager.getCustomFieldManager()

CustomField fieldobj = customFieldManager.getCustomFieldObject(field)

counter = issue.getCustomFieldValue(fieldobj).floatValue()
counter+=1
issue.setCustomFieldValue(fieldobj, counter)

This widget could not be displayed.

Some minor updates to Bhupesh's code above, updating it for v6.2.7, and possibly higher:

 

 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.fields.CustomField

MutableIssue issue = issue

CustomField fieldobj = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("myFieldName")

counter = (float) issue.getCustomFieldValue(fieldobj)
counter+=1
issue.setCustomFieldValue(fieldobj, counter)

 

 

Suggest an answer

Log in or Sign up to answer
Atlassian Summit 2018

Meet the community IRL

Atlassian Summit is an excellent opportunity for in-person support, training, and networking.

Learn more
Community showcase
Posted Wednesday in New to Jira

Are you planning to trial, or are currently trialling Jira Software? - We want to talk to you!

Hello! I'm Rayen, a product manager at Atlassian. My team and I are working hard to improve the trial experience for Jira Software Cloud. We are interested in   talking to 20 people planning t...

92 views 1 0
Join discussion

Atlassian User Groups

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

Find a group

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

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you