Document toolboxDocument toolbox

Copy Projectrak DC field to issues custom field

Hosting

DATA CENTER

Problem

Projectrak DC has an useful feature than allows to map project fields into issues as a custom field.
This feature is not available in Projectrak Cloud and will be lost when migrating from DC to Cloud.

An interesting alternative in cloud could be to activate a Projectrak field as project property, that can be used in Jira expresions to perform searches or workflow conditions: Set up project fields as project properties

This feature can avoid the need to have project information at the issue level in Jira Cloud. But if not possible, it’s necessary to copy the Projectrak field into issues as a custom field before migrating to Cloud.

The following Groovy script to run from the Projectrak DC script console, perform a copy of a Projectrak list field into all projects’s issues as a custom field:

import com.atlassian.jira.component.ComponentAccessor import com.atlassian.jira.bc.issue.search.SearchService import com.atlassian.jira.jql.builder.JqlQueryBuilder import com.atlassian.jira.web.bean.PagerFilter import com.atlassian.jira.issue.Issue import com.atlassian.jira.issue.MutableIssue import com.atlassian.jira.issue.CustomFieldManager import com.atlassian.jira.issue.fields.config.manager.FieldConfigSchemeManager import com.atlassian.jira.event.type.EventDispatchOption import com.deiser.jira.profields.api.field.FieldService import com.deiser.jira.profields.api.value.ValueService import com.deiser.jira.profields.api.field.list.multiple.ListMultipleField import com.atlassian.jira.issue.customfields.option.Option import org.apache.log4j.Logger def errors = "" def logger = Logger.getLogger("MY_SCRIPT") try{ def projectManager = ComponentAccessor.projectManager def searchService = ComponentAccessor.getOSGiComponentInstanceOfType(SearchService.class) def customFieldManager = ComponentAccessor.getCustomFieldManager() def issueManager = ComponentAccessor.getIssueManager() def fieldConfigSchemeManager = ComponentAccessor.fieldConfigSchemeManager def user = ComponentAccessor.jiraAuthenticationContext.getLoggedInUser() def projectrakFieldService = ComponentAccessor.getOSGiComponentInstanceOfType(FieldService.class) def projectrakValueService = ComponentAccessor.getOSGiComponentInstanceOfType(ValueService.class) def JIRA_CUSTOM_FIELD_ID = <JIRA_CUSTOM_FIELD_ID> def jira_custom_field = customFieldManager.getCustomFieldObject(JIRA_CUSTOM_FIELD_ID) def jira_custom_field_options = [] fieldConfigSchemeManager.getConfigSchemesForField(jira_custom_field).each{ jira_custom_field_options.add( ComponentAccessor.optionsManager.getOptions(it.oneAndOnlyConfig).collect{it} ) } def List<Option> new_jira_custom_field_values def PROJECTRAK_FIELD_ID = <PROJECTRAK_FIELD_ID> def projectrak_field = projectrakFieldService.get(PROJECTRAK_FIELD_ID) def projects = projectManager.getProjects() projects.each{project-> try{ new_jira_custom_field_values = new ArrayList() def projectrak_field_value = projectrakValueService.getValue(project, (ListMultipleField)projectrak_field) for(String value : projectrak_field_value){ new_jira_custom_field_values.add (jira_custom_field_options[0].find { it.value.equals(value) }) } def builder = JqlQueryBuilder.newBuilder() builder.where().project(project.getId()) def query = builder.buildQuery() def results = searchService.search(user, query, PagerFilter.getUnlimitedFilter()) if (results.getTotal()){ def projectIssues = "" for (Issue issue : results.getResults()){ try{ MutableIssue mutableIssue = (issueManager.getIssueByCurrentKey(issue.getKey())) mutableIssue.setCustomFieldValue(jira_custom_field, new_jira_custom_field_values) issueManager.updateIssue(user, mutableIssue, EventDispatchOption.DO_NOT_DISPATCH, false) projectIssues += issue.getKey() + ", " } catch(Exception e){ errors += "<br>ERROR: " + e.getMessage() + "<br>" logger.error(e.getMessage()) } } logger.warn("- Updated project \"" + project.getKey() + "\" issues: " + projectIssues) } } catch(Exception e){ errors += "<br>ERROR: " + e.getMessage() + "<br>" logger.error(e.getMessage()) } } } catch(Exception e){ errors += "<br>ERROR: " + e.getMessage() + "<br>" logger.error(e.getMessage()) } logger.warn("Executed!") return errors.length() ? errors : "Executed!"


Important!

  • Both fields must have the same options.

  • Use the source Projectrak field Id. Don’t use the mapped Jira custom field Id as the source for the copy.

  • This script, log messages in the “atlassian-jira.log” file.

  • Probably you get a timeout error on the browser due to it doesn’t get an answer from the server:
    The script is running on all project issues in the Jira instance and it takes long. But the script should have continued the execution and all the project field should be copied into the custom field on all issues.

Related articles