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

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 vote

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

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.

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.

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 vote

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
Atlassian Community Anniversary

Happy Anniversary, Atlassian Community!

This community is celebrating its one-year anniversary and Atlassian co-founder Mike Cannon-Brookes has all the feels.

Read more
Community showcase
Julia Dillon
Posted Tuesday in Jira

Tell us how your team runs on Jira!

Hey Atlassian Community! Today we are launching a bunch of customer stories about the amazing work teams, like Dropbox and Twilio, are doing with Jira. You can check out the stories here. The thi...

151 views 1 17
Join discussion

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