copy custom field (text) to affects version

Hi - I'm trying to import data from Redmine into JIRA.  After the import, I have a custom field (text) that contains the value for Affects Version.  How do I copy this custom field into Affects Version?  JIRA support asked me to look into ScriptRunner, bur I'm not seeing how to yet

3 answers

1 accepted

1 vote
Accepted answer

I think the idea here is that you import the text field as text initially, and then write a script that you can run as a one-off that will read the field on each issue, find (or create) a matching version for it in the project and then set the affects version to it.  Then delete the text field as you don't need it any more.

Got it!  Instead of using updateIssue, I had to use updateIssueAffectsVersions:

versionManager.updateIssueAffectsVersions(issue, affectsVersions);
        	//issueManager.updateIssue(ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser(), issue, EventDispatchOption.ISSUE_UPDATED, false);

Hi Nic - I tried your suggestion but am still stuck at setting the affectsVersion field. This is the code I have:

import com.atlassian.jira.ComponentManager;
import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.project.version.Version;
import com.atlassian.jira.project.version.VersionImpl;
import com.atlassian.jira.issue.IssueManager;
import com.atlassian.jira.issue.CustomFieldManager;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.event.type.EventDispatchOption;
import com.atlassian.jira.project.Project;
import com.atlassian.jira.project.ProjectManager;
import java.util.ArrayList;
import java.util.Collection;
def projectManager = ComponentAccessor.getProjectManager()
def issueManager = ComponentAccessor.getIssueManager()
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def vfi = customFieldManager.getCustomFieldObjectByName("jiramigration_vfi")
Project project = projectManager.getProjectByCurrentKey("APPMGR");
Collection<Long> projectIssueIds = issueManager.getIssueIdsForProject(project.getId());
log.warn(" Init2 ");
Collection<Version> projVers = project.getVersions();
projVers.each {
    log.warn(it.getProject());
    log.warn(it.getDescription());
    log.warn(it.getId());
    log.warn(it.getName());
}
projectIssueIds.each {
    Issue issue = issueManager.getIssueObject(it);
    Collection<Version> affectsVersions = issue.getAffectedVersions();
    def cur_vfi = issue.getCustomFieldValue(vfi);
    String cur_val = cur_vfi.toString();
    if (affectsVersions == null || !affectsVersions.size()) {
        affectsVersions = new ArrayList<Version>();
        Version verToLog;
        projVers.each {
            if (it.getName() == cur_val)
            {
                log.warn(" Match --- " + it.getName() + " " + cur_val);
                verToLog = it;
            }
        }
        if (verToLog != null)
        {
        	log.warn(" Ver Name " + verToLog.getName());
            affectsVersions.add(verToLog);
        	issueManager.updateIssue(ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser(), issue, EventDispatchOption.ISSUE_UPDATED, false);
        }
   }
    
}

The script runs w/o errors, but the issue does not show the affects Versions field populated.  

Suggest an answer

Log in or Sign up to answer
Community showcase
Published Oct 31, 2018 in Marketplace Apps

Marketplace Spotlight: Zephyr

Hello Atlassian Community! Each month, we run a series of Spotlights to highlight Marketplace vendors and apps that our team thinks this Community would find valuable. In last month's Spotlig...

349 views 0 1
Read article

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