Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

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 Cloud and will be lost when migrating from DC to cloudCloud.

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 cloudCloud. But if not possible, it’s necessary to copy the Projectrak field into issues as a custom field before migrating to cloudCloud.

The following groovy 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:

Code Block
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 has to 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.

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

Filter by label (Content by label)
showLabelsfalse
max5
spacescom.atlassian.confluence.content.render.xhtml.model.resource.identifiers.SpaceResourceIdentifier@140ea
showSpacefalse
sortmodified
reversetrue
typepage
cqllabel in ( "script" , "profields" , "scriptrunner" , "exporter" ) and type = "page" and space = "SKB"
labelsprofields script scriptrunner

...