It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

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

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

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 Sep 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. 

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)

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

Joe Pitt Community Leader Sep 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. 

Hi @atlassian dragon,

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? :)

@Max Foerster {Decadis AG}It should keep the one with higest number; if the issue is deleted its ok if it considera the deleted or not 

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 

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

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 

the rewritten code seems not the correct one I am guessing 

Somehow I fixed the code.

Can you provide the code?

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 Padigala  

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

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

Any thoughts?

@Jaswanth Padigala 

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

And how are you running this script?

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?

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)

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 Oct 15, 2019

@Michelle Douma 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. 

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.

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
Community showcase
Posted in Jira

Demo Den Ep. 7: New Jira Cloud Reports

Learn how to use two new reports for next-gen projects in Jira Cloud:  Cumulative flow diagram and Sprint burndown chart. Ivan Teong, Product Manager, Jira Software, demos the Cumulative ...

273 views 1 2
Join discussion

Community Events

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

Find an event

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

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you