How can you copy a custom field to a system field as a stand-alone groovy script?

Kelly Schoenhofen
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.
August 20, 2013

I'm importing some projects from Fogbugz to Jira, and as part of the import cleanup I want to convert some of the custom fields Atlassian's importer brought over to comments on the issues. Elsewhere on Answers, I found a snippet of a groovy script to set the Description system field from a custom field; I took that snipped and I can change setDescription to setComment, and change the name of their custom field to mine, but I don't want to do this as part of a workflow, I need to do this post-import. So for instance, for all projects and customfield XYZ is not empty, I want to copy customfield XYZ to comments. After the script runs (and updates say, 5000 issues) I intend on deleting the custom fields, then doing the next import of 50 projects.

Here's the snippet I have so far:

IssueInputParametersImpl inputParameters = new IssueInputParametersImpl();
inputParameters.setComment("Custom Field XYZ");
inputParameters.addCustomFieldValue(sourceCustomField, "");
validateResults = issueService.validateUpdate(user, issue.getId(), inputParameters);
if (validateResults.isValid()) {
 
  issueService.update(user, validateResults);
 
}

How would I wrap that in a query and then iteratively go through the results and do that script snippet?

Or, am I going about this all wrong? Any other suggestions?

-Kelly

2 answers

1 accepted

0 votes
Answer accepted
JamieA
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.
August 21, 2013

Your going about it right but you have a way to go... here's some code to get you started. Requires testing, and error handling:

import com.atlassian.jira.bc.issue.search.SearchService
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.jql.parser.JqlQueryParser
import com.atlassian.jira.web.bean.PagerFilter
import org.apache.log4j.Logger

def log = Logger.getLogger("com.onresolve.jira.groovy.MyScript")

def jqlQueryParser = ComponentAccessor.getComponent(JqlQueryParser.class)

def query = jqlQueryParser.parseQuery("project = jra ") // query as you would enter in to issue nav

def searchService = ComponentAccessor.getComponent(SearchService.class)
def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
def results = searchService.search(user, query, PagerFilter.getUnlimitedFilter())
def issueManager = ComponentAccessor.getIssueManager()
def commentManager = ComponentAccessor.getCommentManager()
def customFieldManager = ComponentAccessor.getCustomFieldManager()

def customField = customFieldManager.getCustomFieldObjectByName("BillableStatus") // name of CF

results.getIssues().each {issue ->
    def mutableIssue = issueManager.getIssueObject(issue.id)
    commentManager.create(mutableIssue, user.name, /*comment body:*/ issue.getCustomFieldValue(customField) as String, false )
}

Kelly Schoenhofen
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.
August 21, 2013

This is excellent, thank you very much. It may be quick and dirty, but it worked well. A sample run on our sandbox jira had it find & update 235 issues in under 2 seconds.

JamieA
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.
August 21, 2013

Cool. One piece of advice... I think it might be better to append all the custom field values you don't want to keep to the description. You can use wiki markup to put them in a nice table.

Alexander Richter
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.
August 8, 2016

we have a ServiceDesk customfield of the "Request Participants" fieldtype.

Is there a script solution to copy the values to the system field watchers?

0 votes
JamieA
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.
August 21, 2013

Doing it with a service is fine, it's the "proper" way. My way below is the quick and dirty way. You can wrap your code inside the issues loop in my example if you prefer.

Suggest an answer

Log in or Sign up to answer