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
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
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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? :)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
@Max Foerster - K15tIt should keep the one with higest number; if the issue is deleted its ok if it considera the deleted or not
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Which guide did you follow? What does your rewritten code look like now? In what way(s) is it not working?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
the rewritten code seems not the correct one I am guessing
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Somehow I fixed the code.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Can you provide the code?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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 }
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I am getting the attached error for the new loop statement:
Any thoughts?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Can you post your full code? Also which version of JIRA ?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
And how are you running this script?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.PagerFilterimport 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 = 0def 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;
Any ideas?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
@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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Thanks for the advise. When I changed to an id it worked like a charm!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.