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
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:
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":
Thanks,
Petter Gonçalves - Community Support
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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);
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
Thanks,
Vinoth V
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Nic,
Is this script is worthable to what i am lookoing for.
Thanks,
Vinoth
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
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.