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

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

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.

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 ? 

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.

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?

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.

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.

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

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

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.

 

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.

And how about this? 

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

Please vote for it, thanks!

Suggest an answer

Log in or Sign up to answer
Atlassian Community Anniversary

Happy Anniversary, Atlassian Community!

This community is celebrating its one-year anniversary and Atlassian co-founder Mike Cannon-Brookes has all the feels.

Read more
Community showcase
Julia Dillon
Posted Apr 17, 2018 in Jira

Tell us how your team runs on Jira!

Hey Atlassian Community! Today we are launching a bunch of customer stories about the amazing work teams, like Dropbox and Twilio, are doing with Jira. You can check out the stories here. The thi...

771 views 2 19
Join discussion

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