Copy value of a custom field to a system field

Zenaltra Feyrbrand November 20, 2019

We are trying to consolidate project-specific custom description fields into the 'Description' system field to have a streamlined workflow.

I have read through this existing question about copying system field to custom field, but I cannot get it to work in the other direction: https://community.atlassian.com/t5/Jira-questions/copying-values-of-System-field-to-Custom-field/qaq-p/610962

I believe I've found the correct methods to use by following the answers in this  other question:

https://community.atlassian.com/t5/Answers-Developer-Questions/How-to-update-issue-and-set-assignee/qaq-p/552423

I'm able to get the data from the custom field, but this isn't updating the system field. Could someone help point out how I'm messing up the final line parameters, or if I'm missing some additional input:

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.IssueInputParameters
import com.atlassian.jira.bc.issue.IssueService

import org.apache.log4j.Logger
import org.apache.log4j.Level
log.setLevel(Level.DEBUG)
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 = PACE AND summary ~ testscriptrunner") // 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 Description Value: " + documentIssue.description)
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def customField = customFieldManager.getCustomFieldObjectByName("Story Description");
def newValue = issue.getCustomFieldValue(customField)
log.debug("Current Story Description Value: " + newValue)
def issueService = ComponentAccessor.getIssueService()
def issueInputParameters = issueService.newIssueInputParameters()

//customField.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(customField), documentIssue.description),new DefaultIssueChangeHolder()); // copy description to story description - working

issueInputParameters.setDescription("newValue"); //copy story description to description - not working
}

 

I'm not receiving any error, it just won't update the Description even when setting a dummy value

2 answers

1 accepted

0 votes
Answer accepted
Zenaltra Feyrbrand November 20, 2019

I was able to complete a working script. This will iterate through all issues in the search query to copy "Story Description" custom field to "Description" system field. This should be easy to update to copy any CUSTOM field to a SYSTEM field.

Scriptrunner shows an error at line 36, but it doesn't seem to cause any issue.

Hope this can help someone else:

 

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.IssueInputParameters
import com.atlassian.jira.bc.issue.IssueService

import org.apache.log4j.Logger
import org.apache.log4j.Level
log.setLevel(Level.DEBUG)
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 = TEST AND component = \"KT/Roll out\"") // change query to match the issues you want to update - escape quotes in query with \

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)
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def customField = customFieldManager.getCustomFieldObjectByName("Story Description");
def newValue = issue.getCustomFieldValue(customField)
def issueService = ComponentAccessor.getIssueService()
def issueInputParameters = issueService.newIssueInputParameters()

issueInputParameters.setDescription(newValue); //copy story description to description

//Copying data and Validating Update
def updateValidationResult = issueService.validateUpdate(user, issue.getId(), issueInputParameters);
if(updateValidationResult.isValid()) {
def updateResult = issueService.update(user, updateValidationResult);
log.error("Update Result Done without error. Assignee is " + updateResult.getIssue().getAssignee());
}else{
log.error("Update Validation Result not Valid.");
}
}

0 votes
Pete Singleton
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.
November 20, 2019

Rather than scripting, couldn't you export the Description and Custom field into CSV, then use Excel to combine them into one field, then import again to update the Description?

Zenaltra Feyrbrand November 20, 2019

That was actually the first approach I took but, when importing from csv, all of the formatting was lost. Line breaks, bullets, text format is all gone - it just imports as an unformatted single line.

Is there a way to import and keep the formatting?

Pete Singleton
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.
November 20, 2019

No, unfortunately not, as CSV import is just plain text.  You might be able to import HTML markup, but I've never tried it...

Yashwanth Jakkula November 2, 2022

Hi @Zenaltra Feyrbrand 

 

Where do we execute this script? in Console?

Suggest an answer

Log in or Sign up to answer