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

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?

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)

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 Join to answer
Community showcase
Sarah Schuster
Posted Jan 29, 2018 in Jira

What are common themes you've seen across successful & failed Jira Software implementations?

Hey everyone! My name is Sarah Schuster, and I'm a Customer Success Manager in Atlassian specializing in Jira Software Cloud. Over the next few weeks I will be posting discussion topics (8 total) to ...

2,870 views 12 18
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
Atlassian Team Tour

Join us on the Team Tour

We're bringing product updates and pro tips on teamwork to ten cities around the world.

Save your spot