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

How to create a autoincrement custom field in Jira project? Edited

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

Thanks @Joe Pitt for your suggestion.

I'll try it in test

0 votes
Sid Community Leader Dec 15, 2019

@Sekhar Chandra ,

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

@Sid 

 

In Script listener

Suggest an answer

Log in or Sign up to answer
TAGS

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