Copy Affects Version and Fix Version values to new Version custom field

Debraj neogi February 18, 2016

We have a migration project underway. The requirement is to create a new Version field, copy the values of Affects version AND Fix version to this new field. We tried workflow post function (Copy field to field), but it works only for values of 1 field at a time. Can't make both the values go into the new field.

 

Any suggestion would be greatly appreciated. Thanks.

6 answers

0 votes
Vera Henrichs
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
February 29, 2016

And how about this? 

https://jira.atlassian.com/browse/JRA-59992 

Please vote for it, thanks!

0 votes
Jeremy Gaudet
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
February 18, 2016

Give this a try:

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.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;

IssueManager issueManager = ComponentManager.getInstance().getIssueManager();
ProjectManager projectManager = ComponentManager.getInstance().getProjectManager();
CustomFieldManager customFieldManager = ComponentManager.getInstance().getCustomFieldManager();
CustomField nvcf = customFieldManager.getCustomFieldObjectByName("New Version Field Name"); // New Version Custom Field

Project project = projectManager.getProjectByCurrentKey("PROJECTKEY");
Collection<Long> projectIssueIds = issueManager.getIssueIdsForProject(project.getId());

projectIssueIds.each {
    Issue issue = issueManager.getIssueObject(it);
    Collection<Version> fixVersions = issue.getFixVersions();
    Collection<Version> affectsVersions = issue.getAffectedVersions();
    Collection<Version> newFieldVersions = issue.getCustomFieldValue(nvcf);
    if (newFieldVersions == null || !newFieldVersions.size()) {
        newFieldVersions = new ArrayList<Version>();
        newFieldVersions.addAll(fixVersions);
        newFieldVersions.addAll(affectsVersions);
        issue.setCustomFieldValue(nvcf,newFieldVersions);
        issueManager.updateIssue(ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser(), issue, EventDispatchOption.ISSUE_UPDATED, false);
    }
}

I haven't been able to test it because I don't have a project suitable for adding a new version field to offhand, but it should be pretty close.  If you get any exceptions, let me know and I'll take a look.  Just change the two quoted strings to match your project and custom field name.

Debraj neogi February 18, 2016

I am really thankful to you. smile Will surely give it a try and revert for any questions. Thanks again, Jeremy.

Jeremy Gaudet
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
February 18, 2016

Oops, I found an error from copying and pasting out of a sub-task loop.  Fixed.

Debraj neogi February 22, 2016

Hi Jeremy,

Thanks a lot for your help. Hope you are doing fine.

I got timeouts while trying the script, hence thought of checking with you once if you can help.

 

What I am doing:

I have a Deferred to Deferred transition. So any issue that is in Deferred I am pushing it to Deferred status again using a Defer transition button.

For this transition, I go to Post-function, Check Script Post-function --> Custom script post-function and paste the code here.

However I can see errors (red cross-marks) against the following lines, although it allows me to save the script:

 

IssueManager issueManager = ComponentManager.getInstance().getIssueManager();
ProjectManager projectManager = ComponentManager.getInstance().getProjectManager();
CustomFieldManager customFieldManager = ComponentManager.getInstance().getCustomFieldManager();

Collection<Version> newFieldVersions = issue.getCustomFieldValue(nvcf);

issueManager.updateIssue(ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser(), issue, EventDispatchOption.ISSUE_UPDATED, false);

 

I save it, Open Deferred issues for the project, click Defer on 1 of them and it does nothing till there is a timeout error message.

Pretty sure I am bothering you a bit too much now, but hope you can suggest something to get this working.

 

Thanks again.

 

Jeremy Gaudet
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
February 22, 2016

The script isn't for use in a post function of a transition, it is expected to be run as an ad-hoc script in the Script Runner script console.   You can access the console via "JIRA Administration / Add-ons", then "Script Console" under "SCRIPT RUNNER" along the left hand side.

SSchoepel October 29, 2018

I know this is years past when this question was asked but I need exactly this script.

I'm not a programmer so I cannot figure out why when I paste it in I get an error at line 28, character 41. It doesn't like the &lt. What can I do to fix that?

newFieldVersions = new ArrayList&lt;Version&gt;();

Jeremy Gaudet
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
January 3, 2019

Those &lt; and &gt; are "less than" and "greater than" represented in an html encoding.  I'm not sure how they got in there, I've edited it to replace them with the correct characters.

Hung Nguyen August 10, 2020

Thank you for Jeremy for the code.

With newer version of JIRA, I had to replace ComponentManager by ComponentAssessor and changed some of the code to match.

This is the updated version:

import com.atlassian.jira.component.ComponentAccessorimport com.atlassian.jira.issue.Issue;import com.atlassian.jira.project.version.Version;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;

IssueManager issueManager =  ComponentAccessor.getIssueManager();
ProjectManager projectManager = ComponentAccessor.getProjectManager();
CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager();

CustomField nvcf = customFieldManager.getCustomFieldObjectsByName("<Your New field>")[0]; // New Version Custom Field
Project project = projectManager.getProjectByCurrentKey("<YOURPROJECT>");
Collection<Long> projectIssueIds = issueManager.getIssueIdsForProject(project.getId());

projectIssueIds.each {
   
Issue issue = issueManager.getIssueObject(it);
    Collection<Version> fixVersions = issue.getFixVersions();
    Collection<Version> affectsVersions = issue.getAffectedVersions();
    Collection<Version>  newFieldVersions = issue.getCustomFieldValue(nvcf) as Collection<Version>;
    if (newFieldVersions == null || !newFieldVersions?.size()) {
        newFieldVersions = new ArrayList<Version>();
        newFieldVersions.addAll(affectsVersions);
        newFieldVersions.addAll(fixVersions );
        issue.setCustomFieldValue(nvcf,newFieldVersions);
        issueManager.updateIssue(ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser(), issue, EventDispatchOption.ISSUE_UPDATED, false);

    }
}

And I could run it using Script Console.

Please let me know if there is anything we can improve from here.

0 votes
Jeremy Gaudet
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
February 18, 2016

I think the cleanest approach is to use the Script Runner script console; doing it that way removes the need for workflow transitions or bulk updates, you'd just paste the code into the console, set the project as a named string, and run it.  I have most of the necessary code already written across two other similar functions.  If you like, I can probably throw this together for you in fairly short order.  Do you have a test instance to test it out on?  If not, I can provide a version that only performs on individual issues, so you can check it out that way.

I've had to do version migration in the past, and wound up doing it via SQL; having a generic ad-hoc script to do it from the console would likely be generally useful anyways.  How many projects do you need to migrate?

Debraj neogi February 18, 2016

Hi Jeremy,

That would really be wonderful. Yes, we are running a mock migration on stage. So no issues with a bulk change script. We are working on only 1 project here. Thank you, Sir.

0 votes
Debraj neogi February 18, 2016

Hi Boris,

Thanks for responding. Yes, we are using post-functions in our workflow. From Open to Open, In Progress to In Progress, etc. And then on each transition, we are using Copy Field to Field post-function. The issue here is if we have 2 post-functions (The field Temp Version will take the value from Affects Version/s. Source and destination issue are the same. AND The field Temp Version will take the value from Fix Version/s. Source and destination issue are the same.) the Temp Version field only takes value from Affects Version/s. Our requirement is to merge both the fields values to one temporary field and get rid of either of Affects or Fix Version field.


Hope this clarifies. Thanks again for your assistance.

0 votes
Boris Georgiev _Appfire_
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
February 18, 2016

Can you provide more details on what do you mean by "migration" ? How using a post-function helps copying all the values - I suppose you still need to execute a transition for all the issues - are you planning to bulk edit all the issues and execute a transition as part of the bulk edit ? 

0 votes
Boris Georgiev _Appfire_
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
February 18, 2016

You can create a script postfunction using either JJUpin or ScriptRunner and write a script that copies the values from the affects and fix versions to the new field.

Suggest an answer

Log in or Sign up to answer