copy custom field (text) to affects version

Yamuna Ramasubramaniyan April 29, 2016

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
Answer accepted
Nic Brough -Adaptavist-
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
April 30, 2016

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.

1 vote
Yamuna Ramasubramaniyan May 1, 2016

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);
0 votes
Yamuna Ramasubramaniyan May 1, 2016

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
TAGS
AUG Leaders

Atlassian Community Events