copying values of System field to Custom field

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

4 answers

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)

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

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. 

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

}

 

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
}

 

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

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);

}

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

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

Hi Joshua,

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

Thanks,

Vinoth V

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

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 

Suggest an answer

Log in or Sign up to answer
How to earn badges on the Atlassian Community

How to earn badges on the Atlassian Community

Badges are a great way to show off community activity, whether you’re a newbie or a Champion.

Learn more
Community showcase
Published Monday in Jira Software

How large do you think Jira Software can grow?

Hi Atlassian Community! My name is Shana, and I’m on the Jira Software team. One of the many reasons this Community exists is to connect you to others on similar product journeys or with comparabl...

377 views 4 10
Read article

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you