Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

I need to create/add a new field to the issue's default screen

YM January 10, 2021

I would like to get an expert opinion on a dilemma I am facing.

I am using Jira V8.13.1

I need to create/add a new field to the issue's default screen .

The following is an example of what I want to achieve :

 

For example a prefilled dynamic field with alphanumeric ID similar to  SOM21001  where SOM indicates the project name 21 indicates the year and 001 indicates the dynamically incremented number that updates with every new issue. The year is incremented every twelve months and last three digits get incremented by one  for every created issue  001 002 003 …

 

I Would appreciate a detailed answer

1 answer

1 accepted

0 votes
Answer accepted
Radek Dostál
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.
January 10, 2021

Hi Yahya,

 

I'm a bit unclear on what exactly you want out of this - but I will go with what I think or how I understood it (I suspect that the issue screen is irrelevant as part of the question).

 

So you're looking to have an automated field, let's call it 'Dynamic Field' -- this is read-only, not to be manually tampered with, and gets a value automatically based on the calculation as you put it:

 - ${projectName}${last2digisOfCurrentYear}${issueNumber}

 

Where issue number only can have 999 values at most due to being constrained to 3 digits, starting from 001-999.

 

So far so good, sounds like either a Script Field or this could be a regular text-field provided a good post-function on the 'Create', or through perhaps an Automation rule.

I would guess the logic in it could be similar to..

 - get project object of this issue, get the project's name as "$projectName"

- get the current year's last 2 digits

- get this issue's number and format it to the 3-digit format

What if the issue's number is equal/greater than 1,000?

 

Can you list your available plugins if you already have something that could be used?

 

Thanks,
Radek

YM January 10, 2021

Highly appreciated Radek,

 

You have understood me well ...

For what I do, the issue number will never get beyond 1000.  

Now how can achieve this ?

How can I get this  scripted ?  coded ?

I am on a standalone network with no access to the internet ...

Radek Dostál
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.
January 10, 2021

You need to have a plugin to get dynamically generated values in a field on Server - so that's what I wanted to check first if you have any installed we can use.

Radek Dostál
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.
January 10, 2021

You can find those in Manage Apps -> toggle 'User-installed plugins'. A screenshot will be fine no need to know the exact versions.

YM January 11, 2021

Thanks Radek,

The plugins  that is installed ScriptRunner.

This is a standalone network and screenshot is not possible.

Radek Dostál
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.
January 12, 2021

ScriptRunner's probably the best option :)

 

I can send you a sample you can try with some how-to setup when I have a little more free time on my hands (or unless someone else wants to have a knack at it).

 

Before that just to give you an idea of what I have in mind:

 - creating a standard single-line text-field

 - adding that text-field to 'View' screen only (so that users don't fill it out on create/edit, but can see and filter for it)

 - adding a workflow post-function on create to set the field's value according to above logic (https://scriptrunner.adaptavist.com/6.16.0/jira/recipes/workflow/postfunctions/set-issue-attributes.html)

 

With this the field should be getting those values during each Issue Create, be searchable, and dynamic so you won't need any hardcoded input in it. If you're not familiar with groovy and how to code it, I would wholeheartedly recommend trying it out, it's usefulness is seriously underrated. Can write that script later, should be a short one, but anyway would be good to give it a shot meanwhile - you'll need to know the basics to maintain it over time.

Radek Dostál
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.
January 13, 2021

So here goes:

 

 - Create/Use a Text-Field (custom field)

 - Add this field to any screens you like where you would like to see it (but do not place it on Create/Edit if you don't want users to modify it manually)

 - In the workflow where you want this to to be automatically filled out, modify the 'Create' transition and add the following ScriptRunner post-function to it:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.user.ApplicationUser

ApplicationUser currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()

CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()
IssueManager issueManager = ComponentAccessor.getIssueManager()

// Change the custom field name
CustomField customFieldToStoryThisValueIn = customFieldManager.getCustomFieldObjectsByName("<CF NAME>")?.first()
if (customFieldToStoryThisValueIn == null) {
log.error("<CF NAME> not found on the system!")
return
}

String projectName = issue.getProjectObject().getName()
// or maybe you meant key?
// String projectKey = issue.getProjectObject().getKey()

String lastTwoDigitsOfCurrentYear = Calendar.getInstance().get(Calendar.YEAR).toString().substring(2) //2021 -> 21

String issueNumInThreeDigitFormat = String.format("%03d", issue.getNumber())
// Note that if issuenum is 100 and more (i.e. has 3 or more digits already), then this will not do anything
// this only works for adding zeros to 1-99 issue numbers to a 3-digit format

String result = projectName + lastTwoDigitsOfCurrentYear + issueNumInThreeDigitFormat
issue.setCustomFieldValue(customFieldToStoryThisValueIn, result)
issueManager.updateIssue(currentUser, (MutableIssue) issue, EventDispatchOption.DO_NOT_DISPATCH, false)

 - It must be after 'Creates the issue originally' and before 'Re-index an issue', similarly to:

pf.PNG

Make sure to publish and whenever you create a new issue using this workflow then it should be getting the value stored automatically.

 

Now just to re-iterate it's not quite good to keep on adding inline scripts on the instance, but that's the length to go to to get some custom scripted values in there.

Yahya
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
January 15, 2021

Highly appreciated  Radek, 

I will give it a go and get back to you 

YM January 18, 2021

Thanks very much Radek,

This code worked for me with minimum adjustment.

YM January 18, 2021

Can anyone help please 

As i said earlier that this code worked for me,

I need to add two actions to this field in order to protect it from editing by users.

1st to grey out the field (when creating a new issue) so user cannot enter any value into it because the value will be generated internally as a result of the script.

2nd to display the generated value  in the greyed out field ( Read-only). 

 

Could someone help  please ?

Suggest an answer

Log in or Sign up to answer