In many cases you will want to make an exact copy of a record. In this article, we will be discussing about the options that you have, to exactly do that.
For simple entity records and when I say “simple” it means that you have an entity with some native fields as text, decimal, whole number, lookup and so on but we are not talking about many to many (N:N) relationships or many to one relationships (N:1), you could use native process workflows.
These are the steps:
For this example I will use a Quote entity:
Create a simple workflow:
And the mapping:
When you run an on-demand workflow under an existing record, the result will:
It was very simple, but what pass if you are trying to clone the quote and the quote details?
Remember that the quote details are a related entity. We can´t use a workflow to do that.
In this case, you will need to write a few lines of code to handle this situation.
I have designed a solution that involves a native process and a c# plugin to clone this kind of records and details.
1. Create a flag field into the entity that you need to clone called “CloneRecord” of boolean type
2. Create an On-Demand workflow that should fire for the entity that you are trying to copy
a. This workflow set the “CloneRecord” field to “True”
3. Develop and register a CRM c# plugin on the Update of that field.
a. The plugin use the “EntityExtensions.Clone” method of the SDK to accelerate the copy of the entity
b. You will need to clone the “header” and “details” of the quote (for our example).
c. You should go and clone each detail records associated with header.
d. You need to know that when you clone a record with this method of the sdk, the "ID" ( guid ) is copied too. You need to remove this property of the attributes collection before create the record. If you forgot do this, the CRM Service Organization will return an error of duplicated key!
With this example you will be able to create the quote header. In order to clone details you should do this:
Thanks for reading this tutorial. The objective of writing this post is to discuss about different ways to clone entity records. The conclusion has been drawn after practical research and implementation by Microsoft Dynamics CRM online team at Aegis SoftTech.
For further information, mail us at email@example.com