copying values of System field to Custom field

Vinoth V July 18, 2017

We have a requirement where we have to copy the value of 'Summary' field (which is a System field in JIRA) to 'Story Name' field (which is a custom field). This copy process have to be done for about 2000 + issues.

So we are looking for an approach to do this.

Thanks,
Vinoth

5 answers

2 accepted

0 votes
Answer accepted
Petter Gonçalves
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
September 16, 2020

Update in September 2020:

Besides all the options provided below using Scriptrunner for Jira Server, Jira Cloud now have an Automation feature that allows you to properly copy system fields values to custom fields when a specific action is triggered:

Jira Automation 

To provide a practical example, let's suppose you want a system field (Summary) to be copied to a custom field (Text type) when an issue is transitioned to status "Done":

  1. Navigate to the Project related to the issue > Project settings > Automation
  2. Create a new automation rule following the template below:
    Screen Shot 2020-09-16 at 18.51.08.png
  3. Optionally, use the bulk operation to run the action on all the issues you want to be updated

Thanks,

Petter Gonçalves - Community Support

Karl Samson October 25, 2022

Hi Petter, I have tried to follow your template Automation above, but I'm trying to copy the Component/s field for an issue, to a custom field. However, the 'Component/s' field is not listed as a field I can copy!? Please suggest a solution.

Thanks.

0 votes
Answer accepted
Joshua Yamdogo @ 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.
July 18, 2017

Hi Vinoth,

You can run the following script in the script console to update all of the custom fields for specific issues. Just replace the JQL query with one that will match the issues you're trying to replace, e.g. "type = story".

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.search.SearchProvider
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.jql.parser.JqlQueryParser
import com.atlassian.jira.web.bean.PagerFilter

def jqlQueryParser = ComponentAccessor.getComponent(JqlQueryParser.class)
def searchProvider = ComponentAccessor.getComponent(SearchProvider.class)
def issueManager = ComponentAccessor.getIssueManager()
def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()

def query = jqlQueryParser.parseQuery("project = JRTWO") // change query to match the issues you want to update

def results = searchProvider.search(query, user, PagerFilter.getUnlimitedFilter()) // get results of query

results.getIssues().each {documentIssue -> // go through each issue
def issue = issueManager.getIssueObject(documentIssue.id)
//log.debug("-----------------------")
//log.debug(documentIssue.key)
//log.debug("Current Summary Value: " + documentIssue.summary)
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def customField = customFieldManager.getCustomFieldObjectByName("TextFieldA");
customField.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(customField), documentIssue.key),new DefaultIssueChangeHolder()); // update values
}

 

Vinoth V July 19, 2017

Hi Joshua,

The script is working fine and all the Story Names are filled but the issue is, when we are searching for issues like project= xyz and issuetype= story and "Story Name" is EMPTY , still it it is showing issues.
Ex: When we open one issue, that issue of story name was already  filled but the query what we have searched is story name is Empty.

The query is not working for what i am looking for.

Thanks,
Vinoth V

Joshua Yamdogo @ 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.
July 19, 2017

Hey Vinoth,

I see two problems.

First, are you using the escape character '\' whenever you use quotes? When writing a JQL query inside of a script, you'll need to do that. For instance, your query would like like this:

def query = jqlQueryParser.parseQuery("project= xyz and issuetype= story and \"Story Name\" is EMPTY)

The second problem is that the issues need to be reindexed after their values are updated. I added a loop to the script that will go through all of the issues returned from the JQL and reindex them after their value gets updated. After testing this, I no longer see issues which do not match the query.

import com.atlassian.jira.component.ComponentAccessor

import com.atlassian.jira.issue.ModifiedValue

import com.atlassian.jira.issue.search.SearchProvider

import com.atlassian.jira.issue.util.DefaultIssueChangeHolder

import com.atlassian.jira.jql.parser.JqlQueryParser

import com.atlassian.jira.web.bean.PagerFilter

import com.atlassian.jira.issue.Issue

import com.atlassian.jira.issue.index.IssueIndexManager

import com.atlassian.jira.util.ImportUtils


def jqlQueryParser = ComponentAccessor.getComponent(JqlQueryParser.class)

def searchProvider = ComponentAccessor.getComponent(SearchProvider.class)

def issueManager = ComponentAccessor.getIssueManager()

def indexManager = ComponentAccessor.getComponent(IssueIndexManager)

def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()


def query = jqlQueryParser.parseQuery("project = JRTWO and \"TextFieldA\" is EMPTY") // change query to match the issues you want to update

def results = searchProvider.search(query, user, PagerFilter.getUnlimitedFilter()) // get results of query

results.getIssues().each {documentIssue -> // go through each issue

    log.debug(documentIssue.key)

    def issue = issueManager.getIssueObject(documentIssue.id)

    def customFieldManager = ComponentAccessor.getCustomFieldManager()

    def customField = customFieldManager.getCustomFieldObjectByName("TextFieldA");

    customField.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(customField), documentIssue.key),new DefaultIssueChangeHolder()); // update values

}

List issues = results.getIssues();

for (Issue issue in issues) {

    boolean wasIndexing = ImportUtils.isIndexIssues();

    ImportUtils.setIndexIssues(true);

    indexManager.reIndex(issueManager.getIssue(issue.id));

    ImportUtils.setIndexIssues(wasIndexing);

}
Vinoth V July 19, 2017

Hi Joshua,

Now the script and query is working fine but i am getting the following warnings multiple times.
Please find the attached screenshot for the actual warning message.

Log error.JPG

Thanks,

Vinoth V

Joshua Yamdogo @ 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.
July 20, 2017

Hi Vinoth,

Glad to hear that it works!

That error message you're getting seems to be coming from com.innovalog, so this doesn't look like a ScriptRunner problem. It looks like you're using the JIRA Misc Custom Fields plugin. From what the error is saying, you need to go to the Transition Info custom field and fix the description for it. 

I would post a new question using the 'addon-com.innovalog.jmcf.jira-misc-custom-fiel' tag or perhaps make a new ticket on the Innovalog support portal.

If you're happy with the script I gave you, feel free to select it as your accepted answer.

Thanks,

Joshua

Vinoth V July 20, 2017

Hi Joshua,

I had added the description for Transaction info custom field and there are no warnings and no issues.

Thanks,

Vinoth V

Joshua Yamdogo @ 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.
July 20, 2017

Hi Vinoth,

I'm happy everything is working now! Feel free to select my answer as the accepted answer for this question so that others will see it if they have the same issue.

Thanks for using ScriptRunner! :)

Regards,

Josh

Kranti September 17, 2018

my requirement is:  While creating a Jira ticket, I want to copy a System field value to a custom a field. e.g Summary value to release notes field.  I want this copy happen while creating the ticket itself. can this script server my purpose..if not could you please help me out on this.?

0 votes
Manideep Sadhu March 14, 2018

HI 

I need to copy duedate system field to one of my custom field using script can you please post the complete code to just copy the value

Thanks 

0 votes
Joshua Yamdogo @ 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.
July 18, 2017

Hi Vinoth,

You can run the following script in the Script Console to search for specific issues and replace all custom field values. Just change the JQL query to match whatever issues you're trying to find, e.g. "type=story".

import com.atlassian.jira.component.ComponentAccessor

import com.atlassian.jira.issue.ModifiedValue

import com.atlassian.jira.issue.search.SearchProvider

import com.atlassian.jira.issue.util.DefaultIssueChangeHolder

import com.atlassian.jira.jql.parser.JqlQueryParser

import com.atlassian.jira.web.bean.PagerFilter



def jqlQueryParser = ComponentAccessor.getComponent(JqlQueryParser.class)

def searchProvider = ComponentAccessor.getComponent(SearchProvider.class)

def issueManager = ComponentAccessor.getIssueManager()

def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()


def query = jqlQueryParser.parseQuery("project = JRTWO")


def results = searchProvider.search(query, user, PagerFilter.getUnlimitedFilter()) // get results of query


results.getIssues().each {documentIssue ->; // go through each issue

def issue = issueManager.getIssueObject(documentIssue.id)

//log.debug("-----------------------")

//log.debug(documentIssue.key)

//log.debug("Current Summary Value: " + documentIssue.summary)

def customFieldManager = ComponentAccessor.getCustomFieldManager()

def customField = customFieldManager.getCustomFieldObjectByName("TextFieldA");

customField.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(customField), documentIssue.key),new DefaultIssueChangeHolder()); // update values

}

 

0 votes
Nic Brough -Adaptavist-
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
July 18, 2017

I've got this snippet for script-runner.

It's a post-function, so it assumes you have the issue to amend as a variable "issue".  It also assumes your target field is a text field, as it takes summary as a string.

import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.component.ComponentAccessor

def customFieldManager = ComponentAccessor.getCustomFieldManager()
def targetField = customFieldManager.getCustomFieldObjects(issue).find {it.name == "My CustomField"}
def changeHolder = new DefaultIssueChangeHolder()
targetField.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(targetField), issue.summary),changeHolder)

Vinoth V July 18, 2017

Hi Nic,

  • Do we need to add the script in the behoviour itself or else any other way ?
  • if it is for specific issue type[ex:story], we need to copy the value of 'Summary' field (which is a System field in JIRA) to 'Story Name' field (which is a custom field) for all the issues. 
  • if we need for other issue type[ex:epic],we need copy the value of 'Summary' field (which is a System field in JIRA) to 'Epic Name' field (which is a custom field)for all the issues.

Is this script is worthable to what i am lookoing for.

Thanks,

Vinoth

Nic Brough -Adaptavist-
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
July 18, 2017

Behaviours have nothing to do with this.

The snippet is a post-function to give you the basic code for copying from summary to another field.  You should be able to adapt it for your needs quite easily (loop through your list of issues that need changing and running the snippet on each issue.  An "if" statement on the issue type will let you select which field to update).

One note of caution - when copying to an Epic Name field, it is going to fail, unless you genuinely have an existing Epic with exactly the value of the summary. 

Suggest an answer

Log in or Sign up to answer