Defect #11315

When I copy a project it does not copy the issues custom fields' values

Added by Felipe Garcia over 5 years ago. Updated 8 months ago.

Status:Needs feedbackStart date:
Priority:NormalDue date:
Assignee:-% Done:

0%

Category:Custom fields
Target version:-
Resolution: Affected version:

Description

We created a template project, but when we try to copy that project, the issues custom fields' values does not get copied.

Why this is happening? I am using Redmine 1.4


Related issues

Related to Redmine - Defect #13518: To copy project doesn't copy the issues' custom fields in... Needs feedback
Related to Redmine - Defect #13540: To copy project doesn't copy the issues' custom fields in... Closed
Related to Redmine - Defect #11481: Copying Projects (with Tickets) not working Needs feedback
Related to Redmine - Defect #22342: When copying issues to a different project, subtasks /w c... Closed

History

#1 Updated by Felix Schäfer about 5 years ago

This happens when issues are bulk copied and the tracker and/or the project is changed. In these cases the custom field values just get deleted.

Here's a patch that will keep applicable custom field values instead of deleting them all:

diff --git a/app/models/issue.rb b/app/models/issue.rb
index 53d7823..9c71538 100644
--- a/app/models/issue.rb
+++ b/app/models/issue.rb
@@ -228,7 +228,7 @@ class Issue < ActiveRecord::Base
   def tracker_id=(tid)
     self.tracker = nil
     result = write_attribute(:tracker_id, tid)
-    @custom_field_values = nil
+    recalculate_custom_field_values!
     result
   end

@@ -257,7 +257,7 @@ class Issue < ActiveRecord::Base
       if parent && parent.project_id != project_id
         self.parent_issue_id = nil
       end
-      @custom_field_values = nil
+      recalculate_custom_field_values!
     end
   end

diff --git a/vendor/plugins/acts_as_customizable/lib/acts_as_customizable.rb b/vendor/plugins/acts_as_customizable/lib/acts_as_customizable.rb
index cfee2c5..d1db757 100644
--- a/vendor/plugins/acts_as_customizable/lib/acts_as_customizable.rb
+++ b/vendor/plugins/acts_as_customizable/lib/acts_as_customizable.rb
@@ -153,6 +153,16 @@ module Redmine
           @custom_field_values_changed = true
         end

+        # Recalculate custom_field_values based on current available_custom_fields.
+        # We need to make sure no extraneous custom_field_values are saved,
+        # #custom_field_values= will only save values for custom fields available
+        # to this object.
+        def recalculate_custom_field_values!
+          cfv_hash = custom_field_values.inject({}) {|h,cfv| h[cfv.custom_field.id] = cfv.value; h}
+          reset_custom_values!
+          self.custom_field_values = cfv_hash
+        end
+
         module ClassMethods
         end
       end

#2 Updated by Toshi MARUYAMA about 5 years ago

  • Category set to Custom fields

#3 Updated by Toshi MARUYAMA about 4 years ago

  • Related to Defect #11481: Copying Projects (with Tickets) not working added

#4 Updated by Mischa The Evil 8 months ago

  • Status changed from New to Needs feedback

I just came around this issue again and checked whether this issue still persists on current trunk (at r16339). AFAICS, this is fixed by r15318 for #22342. I did some quick tests and wasn't able to reproduce the issue any longer.
Can this be confirmed by somebody?

#5 Updated by Mischa The Evil 8 months ago

  • Related to Defect #22342: When copying issues to a different project, subtasks /w custom fields not copied over added

Also available in: Atom PDF