Quick Inquiry

Home >> Articles >> Entity Record Clonation With Dynamics CRM

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.


How you could do that?

These are the steps:

  1. Create an On-Demand workflow that should fire for the entity that you are trying to copy
  2. In the first step, create a record of the same type of entity and map each field from source to target

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.

Let me explain you:

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!


Example:

if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity) { Entity quote = (Entity)context.InputParameters["Target"]; bool check = false; if (quote.Attributes.Contains("new_clonerecord") check = (bool)quote.Attributes["new_clonerecord"]; if (check) { //Get the quote with all fields Entity newQuote = GetQuote(quote.Id, svc); //Get the quote details with all fields EntityCollection quoteProducts = GetProducts(quote.Id, svc); EntityReferenceCollection x = new EntityReferenceCollection(); //This method return a Dynamics CRM Entity with all fields copied Entity clonedQuote = EntityExtensions.Clone(quote, false); clonedQuote.Id = Guid.Empty; clonedQuote.Attributes.Remove("quoteid"); try { clonedQuote.Id = svc.Create(clonedQuote); //Clear the flag field Quote.Attributes["new_clonerecord"] = false; svc.Update(Quote); } catch (Exception ex) { throw new InvalidPluginExecutionException("Error: " + ex.Message); } }

With this example you will be able to create the quote header. In order to clone details you should do this:


foreach (Entity product in quoteProducts.Entities) { Entity clonedProduct = EntityExtensions.Clone(product, false); clonedProduct.Id = Guid.Empty; clonedProduct.Attributes.Remove("quotedetailid"); clonedProduct.Attributes["quoteid"] = new EntityReference( clonedQuote.LogicalName, clonedQuote.Id); try { svc.Create(clonedProduct); } catch (Exception ex) { throw new InvalidPluginExecutionException("Error: " + ex.Message); } }

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 info@aegissofttech.com


Copyright © 2017 - aegisisc, All rights reserved