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

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
How to earn badges on the Atlassian Community

How to earn badges on the Atlassian Community

Badges are a great way to show off community activity, whether you’re a newbie or a Champion.

Learn more
Community showcase
Published May 30, 2018 in Marketplace Apps

Three tips for boosting your board's efficiency with Story Maps

Trello is one of the most effective tools for driving your sprints. It's customizable for every Agile team and product owners and Scrum masters (SM) love it. However, Agile teams often struggle with:...

852 views 2 9
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