[SalesForce] Copy parent case data to child case

I have 50+ fields on case object. There are multiple related/child cases under one parent case. The parent case is populated with some data. Is there a way through which we can copy data from parent case to child case as soon as child cases are created as the parent and child case have same fields?

Note: I already know that it can be done via process builder by using update feature and mapping parent and child fields but it's such a waste of time because I have 50+ fields so it will take along time. Is there a quicker way to just copy data from parent to child?

  1. I also know that I can create formula fields on child cases but it will be unnecessarily repetition of same fields e.g field 'Description' already exists on both Parent and Child case. I just want to copy the data rather than creating a formula field on child case with the name 'Description 2' that references the 'Description' field on parent case.

Kindly let me know if there is a time saving and efficient solution?

Thanks

Best Answer

I'd probably go with a trigger and the sObject.getPopulatedFieldsAsMap() method.

When the child case is created you have the trigger query all the parent cases fields that you want to copy over. Then run through the getPopulatedFieldsAsMap() Map keys and copy the values from the parent case to the child case using .get(field) and .put(field).

It would also be worth bulkifying so that each parent case is only only queried once and then mapped back to the appropriate child case.