Need a custom field that updates the value with an +1 increment when new issue is created

Sid
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.
September 26, 2018

Need a custom field that updates the value with an +1 increment when new issue is created 

eg - issue JIRA - 1 , field X value  -1001, then when JIRA - 2 is created field x values automatically set to 1002 and so on

thanks

 

2 answers

1 accepted

0 votes
Answer accepted
Sid
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.
December 23, 2018

Create a text custom field (single line). In the example I added "Numbers" as field name. Then create a postfuntion with the script below. 

 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.jql.parser.JqlQueryParser
import com.atlassian.jira.web.bean.PagerFilter
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.search.SearchProvider

// get all the used numbers so far
def query = ComponentAccessor.getComponent(JqlQueryParser).parseQuery("project = JA AND 'Numbers' is not EMPTY ORDER BY 'Numbers' DESC")
def results = ComponentAccessor.getComponent(SearchProvider).search(query,ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser(),new PagerFilter(1))
int maxNumbers = 0
CustomField cField = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Numbers")

// get the highest used number
for(Issue documentIssue: results.getIssues())

{ maxNumbers = documentIssue.getCustomFieldValue(cField) as Integer }
// set the new number
def newNumbers = ++maxNumbers as String
issue.setCustomFieldValue(cField, newNumbers)

 

Hope this helps to anyone looking similar type of fields 

0 votes
Joe Pitt
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
September 26, 2018

There may be a plugin. However you need to refine the requirement. Is it by issue type, in project, issue type in project, or all of JIRA instance to find what you're looking for. 

Sid
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.
September 26, 2018

it is by issue in project. Lets say I create an issue with issue type feature, an the field X - will have 1001 (or any number) then the next issue in the same project (any issue type) it would get the field vale 1002 (1 value increment)

Sid
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.
September 26, 2018

@Joe Pitt, Can you let me know the plugin that has this feature

Joe Pitt
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
September 27, 2018

No, I don't use one that does that. I just use jira suite utilities. Just search the marketplace. I know there are some out there that will increment an integer field. 

Max Foerster - K15t
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
September 27, 2018

Hi @Sid,

you can basically have a look at any workflow app available to do this. But what Joe already asked for is important - some more detailed requirements. What happens with the number if you delete issues etc. So what's the overall purpose of this number in your instance? :)

Sid
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.
September 27, 2018

@Max Foerster - K15tIt should keep the one with higest number; if the issue is deleted its ok if it considera the deleted or not 

Sid
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.
October 9, 2018
Sid
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.
August 5, 2019

I have above script running in version 7, but with version 8 I am getting the errors. Tried to change the script according the guide provide but no luck. Can anyone look into it? where I have the mistake? 

@Joanna Choules 

Joanna Choules
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.
August 5, 2019

Which guide did you follow? What does your rewritten code look like now? In what way(s) is it not working?

Sid
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.
August 5, 2019

hi @Joanna Choules ,

here is the guide - https://scriptrunner.adaptavist.com/5.4.49-jira8/jira/releases/UpgradingToJira8.html

 

Gives me the following errors - cannot find the matching method  com.atlassian.jira.issue.search.SearchProvider and cannot loop with element method 

Sid
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.
August 5, 2019

the rewritten code seems not the correct one I am guessing 

Sid
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.
August 6, 2019

Somehow I fixed the code.

Jaswanth P August 6, 2019

Can you provide the code?

Sid
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.
August 7, 2019
def results = ComponentAccessor.getComponent(SearchProvider).search(query,ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser(),new PagerFilter(1))

to

import com.atlassian.jira.bc.issue.search.SearchService

def search = ComponentAccessor.getComponent(SearchService).search(ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser(), query, new PagerFilter(1))

and from

for(Issue documentIssue: results.getIssues())
{ maxNumbers = documentIssue.getCustomFieldValue(cField) as Integer }

to

for(Issue documentIssue: search.results)
{ maxNumbers = documentIssue.getCustomFieldValue(cField) as Integer }

 

@Jaswanth P  

Jaswanth P August 7, 2019

I am getting the attached error for the new loop statement:

2019-08-07_11-42-45.png

Any thoughts?

Sid
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.
August 7, 2019

@Jaswanth P 

Can you post your full code? Also which version of JIRA ?

Sid
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.
August 7, 2019

And how are you running this script?

Aaron Andrade October 11, 2019

I took the script and made suggested changs (Jira 8.3.1)

import com.atlassian.jira.bc.issue.search.SearchService
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.jql.parser.JqlQueryParser
import com.atlassian.jira.web.bean.PagerFilter

import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.MutableIssue

// get all the used numbers so far
def query = ComponentAccessor.getComponent(JqlQueryParser).parseQuery("project = ZTESTB AND 'Number Field' is not EMPTY ORDER BY 'Number Field)' DESC")
def search = ComponentAccessor.getComponent(SearchService).search(ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser(), query, PagerFilter.getUnlimitedFilter())
int maxNumbers = 0

def customFieldManager = ComponentAccessor.getCustomFieldManager()
def cField = customFieldManager.getCustomFieldObjectsByName("Number Field")

for(Issue documentIssue: search.results)
{ maxNumbers = documentIssue.getCustomFieldValue(cField) as Integer }

// set the new number
def newNumbers = ++maxNumbers as String
issue.setCustomFieldValue(cField, newNumbers)

Both of these lines are throwing errors;

  • { maxNumbers = documentIssue.getCustomFieldValue(cField) as Integer } --> Cannot find matching method com.atlassian.jira.issue.Issue@getCustomFieldValue(java.util.Collection ).
  • issue.setCustomFieldValue(cField, newNumbers) --> Cannot find matching method com.atlassian.jira.issue.MutableIssue#setCustomFieldValue(java.util.Collection, java.lang.String)

Any ideas?

Aaron Andrade October 11, 2019

Update:  I just got rid of the errors by changing the following line from...

def cField = customFieldManager.getCustomFieldObjectsByName("Number Field")

to...

def cField = customFieldManager.getCustomFieldObject("Number Field")

But it is setting the Number Field value.  I would expect it to be at least 0 as the default value is being set to zero.  This is the error in my log

java.lang.NullPointerException
at com.atlassian.jira.issue.DocumentIssueImpl.getCustomFieldValue(DocumentIssueImpl.java:350)
at com.atlassian.jira.issue.Issue$getCustomFieldValue$6.call(Unknown Source)
at Script370.run(Script370.groovy:19)

Joanna Choules
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.
October 15, 2019

Hi Michelle,

If you want to use getCustomFieldObject in place of getCustomFieldObjectsByName, then you need to supply the string ID of the field rather than its name. This ID will be of the form customfield_12345, where 12345 is the numerical ID of the field.

Alternatively, you can just use getCustomFieldObjectByName (note the singular 'Object' as opposed to plural 'Objects'). Strictly speaking this is a deprecated method but the likelihood of it being removed entirely any time soon is quite low.

Joanna

Joe Pitt
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 15, 2019

@Aaron Andrade I would caution using @Joanna Choules method. She states ' Strictly speaking this is a deprecated method but the likelihood of it being removed entirely any time soon is quite low.' I have been in the situation in another tool were a method I was using was removed without warning. Since was officially not available the vendor didn't send out a warning when it was removed. Things just broke and I had to scramble to find a new solution. 

Joanna Choules
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.
October 15, 2019

This is just an alternative - obviously the first method I mentioned in that same comment is the preferable one. I believe it is Atlassian policy to mention the removal of API methods in their change notes, even if those methods had already previously been deprecated, so there is an element of forewarning as long as you don't immediately rush into upgrading without checking the notes, but all the same there is an element of risk - as you mention - that has to be weighed up against other considerations.

Aaron Andrade October 15, 2019

Thanks for the advise.  When I changed to an id it worked like a charm!

Suggest an answer

Log in or Sign up to answer