Forums

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

how to get issue key copied to custom field

Tamil June 25, 2020

Hi,

I have a custom field which needs to have issue key in formatted form(e.g if issue key is

TT-41, the value in custom field should be TT41). I have scriptrunner and i have written some code to retrieve issue key but is showing some error. 

Can someone help with my code to get issue key copied to custom field in formatted value?


import com.atlassian.jira.issue.Issue
import com.atlassian.jira.event.*
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.CustomFieldManager;
import com.atlassian.jira.event.issue.DelegatingJiraIssueEvent;
import com.atlassian.jira.event.issue.IssueEvent;
import com.atlassian.jira.event.issue.IssueEventBundle;
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.event.type.EventType;
import org.apache.log4j.Logger
import org.apache.log4j.Level
import groovy.sql.Sql
import java.sql.Driver

import com.atlassian.jira.issue.MutableIssue;

def log = Logger.getLogger("com.acme.CreateSubtask")
log.setLevel(Level.DEBUG)


def customField = ComponentAccessor.getCustomFieldManager().getCustomFieldObject("customfield_10503");// here replace the ID with ID of your custom field.
def issueManager = ComponentAccessor.getIssueManager()
//Issue issue = issue
def issue = issueManager.getIssueObject("TT-51")
def value = issue.getCustomFieldValue(customField);

I have below clarifications

I dono how to retrieve the issue key and copy to custom field?

How to write a condition to trim issue key(e.g TT-41 to TT41) to copy into custom field?

1 answer

1 accepted

0 votes
Answer accepted
Nic Brough -Adaptavist-
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.
June 25, 2020

https://library.adaptavist.com/entity/update-the-value-of-a-custom-field-using-a-listener might be a really good base for your script

I would recommend a look at

  • the requirement - I can't think of a good use for trimming out the -
  • using a "scripted field" instead - the script would be more simple because it becomes "issue.getKey()" and "return reformatted value" without needing to think about writing to the custom field.
Tamil June 25, 2020

Hi Nic,

 

Thanks for the response. For every newly created issue, the issue key has to be formatted and updated in Custom field.(e.g for next issue, issue key is TT-55 then custom field value should have TT55).

 

How could i achieve this formatting of issue key to update in custom field

Nic Brough -Adaptavist-
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.
June 25, 2020

I'd use the script from the library, adjusted to remove the -.  After questioning the point of the requirement.

Tamil June 25, 2020

Hi Nic,

Could you give an example script of removing the '-' after retrieving issue key values.

when i used issue.getKey() i am getting error as 

"[STATIC TYPE CHECKING]- the variable issue is undeclared

But i have imported the class "import com.atlassian.jira.issue.Issue"

 

Could you help on this?

Nic Brough -Adaptavist-
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.
June 25, 2020

Where is your script being run? 

I ask because the "issue" object is only directly available when you are working in the context of an issue.  For example, if your script is a workflow element (post function, condition, validator, etc) or it is a scripted field, then the code is run with the variable "issue" holding the current issue.  If you're in the console or a listener, you have to do some more work to get to an issue object to act upon.

Tamil June 25, 2020

Hi Nic,

 

I have created a custom field and in Behaviour i have added the script.

I couldn't add in workflow post-function as it is shared by many projects. So i have created a behaviour and added script.

 

Is it possible to run the script in behaviour?

Nic Brough -Adaptavist-
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.
June 25, 2020

Yes, but this is clunky, and it will allow people to edit the value.  I'd still strongly recommend a scripted field for this.

Tamil June 25, 2020 edited

Hi Nic,

I created a custom Field and added the script below and mapped to View screen. The issue key is mapped to custom field as "TT-56"

import com.atlassian.jira.issue.Issue
Issue issue=issue
def key=issue.getKey()

 

By adding the scriptline below updates only the value which we defined manually

customField.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(customField), newValue), new DefaultIssueChangeHolder() 

 

Now if i want to write script to trim character(-) and display as(TT**) for every issue(generic case) created what is the script line to be added?

Tamil June 25, 2020

Hi Nic,

I have written in scripted field as below to replace (-) in the issue key. But when i run the code, the output is still showing as "TT-24" without replacing (-)

import com.atlassian.jira.issue.Issue
Issue issue=issue
def key=issue.getKey()

return key.replaceAll(/.-*\[/, '')

 

Could you please help on this?

Tamil June 26, 2020

Hi Nic,

I converted issue key to string and formatted value using replaceALL

Now am getting desired output.

 

Thanks for the help!

 

Regards,

Tamil

Suggest an answer

Log in or Sign up to answer