Updating a custom field by +1 everytime issue is reopened

Bhupesh Nagda July 16, 2014

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

1 vote
Answer accepted
Christian Czaia _Decadis AG_
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
July 17, 2014

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

JamieA
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
July 17, 2014

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.

Bhupesh Nagda August 4, 2014

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

ANKITA BARBI October 4, 2017

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?

Nic Brough -Adaptavist-
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
October 4, 2017

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

ANKITA BARBI October 4, 2017

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.

Nic Brough -Adaptavist-
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
October 4, 2017

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?

1 vote
Scott Geertgens May 29, 2015

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)

 

 
1 vote
Bhupesh Nagda August 4, 2014
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)

Suggest an answer

Log in or Sign up to answer