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.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I am really thankful to you. Will surely give it a try and revert for any questions. Thanks again, Jeremy.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Oops, I found an error from copying and pasting out of a sub-task loop. Fixed.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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 <. What can I do to fix that?
newFieldVersions = new ArrayList<Version>();
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Those < and > 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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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 ?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Atlassian Government Cloud has achieved FedRAMP Authorization at the Moderate level! Join our webinar to learn how you can accelerate mission success and move work forward faster in cloud, all while ensuring your critical data is secure.
Register NowOnline forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.