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?
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
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I'd use the script from the library, adjusted to remove the -. After questioning the point of the requirement.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Yes, but this is clunky, and it will allow people to edit the value. I'd still strongly recommend a scripted field for this.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Online forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.