Entity Record Clonation With Dynamics CRM

banner

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?

Before knowing the steps we need to know what exactly is clone record in Microsoft Dynamics 365 CRM Services.

The feature becomes accessible when you need to make several records with the same data or any alterations that you have to make without affecting the uniqueness. With clone record Dynamics 365, you could simply copy every info through the innovative record, such as fields, associations, and add-ons. Saves time nonetheless it even safeguards correctness and constancy in data entry. Furthermore, the cloned record could be modified to comprise any essential alterations, like updating contact info or adjusting dates.

These are the steps:

  • Create an On-Demand workflow that should fire for the entity privileges that you are trying to copy
  • 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:

entity
Our expert team can help you solve issues by creating C# plugins to streamline your entity record emulating process.

And the mapping:

entity

When you run an on-demand workflow under an existing record, the result will:

entity

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. 1Create 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

    • This workflow set the “CloneRecord” field to “True”

  3. Develop and register a CRM c# plugin on the Update of that field.

    • This workflow set the “CloneRecord” field to “True”

    • The plugin use the “EntityExtensions.Clone” method of the SDK to accelerate the copy of the entity

    • You will need to clone the “header” and “details” of the quote (for our example).

    • You should go and clone each detail records associated with header.

    • 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); } }
Ready to ditch the trial and error and start achieving your goals? Let's work together to find the perfect cloning Entity records!

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); } }
entity

Thanks for reading this tutorial. The objective of writing this post is to discuss different ways to clone entity records. This conclusion has been drawn after practical research and implementation by Microsoft Dynamics CRM Development team at Aegis Softtech, a Java development company.

For further information, mail us at info@aegissofttech.com

Empower your Business with Team Aegis, CONNECT NOW!
captcha

Scale your Business with our Software Services Now!
Related article

In any software product, the aspect of Security perhaps plays the most important part.

It was yesterday when I and one of my colleague was working on Microsoft dynamics CRM and we stuck into a situation where we did not know how to call WebAPI method from CRM custom activity.

More banking institutions are making modern approach for their business operations and choosing Microsoft Dynamics CRM to migrate

DMCA Logo do not copy