How to create a autoincrement custom field in Jira project?

Sekhar Chandra December 13, 2019

Hi All - 

I want to create a custom field that gets incremented by +1 every time whenever a new issue creates in Jira project "SSP" and this field has to be visible on create screen and view screen of the issue. 

Simply saying, the field should replicate the issue number and this should be done automatically by the system. 

I tried with some scripts but getting errors and the script is here 

 

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 = SSP 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
//def issue = ComponentAccessor.getComponent(Issue)
issue.setCustomFieldValue(cField, newNumbers)

 

Please help me out.

 

Thanks, 

Chandra.

 

2 answers

0 votes
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 15, 2019

@Sekhar Chandra ,

Where are you running this script? post functions or script listener?

Sekhar Chandra December 16, 2019

@Sid 

 

In Script listener

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 17, 2019
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.
December 13, 2019

I can't help you with the script, but tying it to the issue ID is going to be a problem. The ID is created at the end of the create process. You would need to run the script in a post function after the issue is created. I haven't tried that so I don't know if it is even possible. 

Sekhar Chandra December 16, 2019

Thanks @Joe Pitt for your suggestion.

I'll try it in test

Suggest an answer

Log in or Sign up to answer