{knowhow} Clone an entity record programmatically in Microsoft Dynamics CRM using Clone method

Recently in my project the customer came up with a requirement where they needed to clone a record programmatically. They wanted a common API which can be used to clone records of any entities.

Normally the requirement would be like there would be a button on the entity record form called ‘Copy Record’ or ‘Clone Record’ and then once you click that, a new form would open up with the data copied from the parent record. I have explained the same stuff in the blog post – https://debajmecrm.com/2015/01/27/how-to-using-xrm-utility-openentityform-to-clone-all-fields-of-one-record-to-another-in-dynamics-crm/.

However this time, the requirement was a bit different. This time the client needed an API which can clone a single entity or a collection of entities. The function would accept either an Entity as parameter or EntityCollection as parameter.

I was thinking if Dynamics CRM provides something like MemberwiseClone method of C#. Could not find initially anything like that initially and decided to write something of my own. But before that, I decided to go back to the CRM programmer’s bible – CRM SDK. And guess what, CRM indeed has a clone method which does exactly the same stuff I was looking.

Below is the screenshot of the method definition from the SDK.

image

As you can see, the method is present in the Microsoft.Xrm.Client.dll.

With the help of this, you could do something like the below to clone a record.

 

var customer = proxy.Retrieve("account", Guid.Parse("2767215B-C2D2-E111-B664-005056AB021D"), "Columns which you want to copy");
var newCustomer = EntityExtensions.Clone(customer, true); // You can also write customer.Clone(true);
newCustomer.Id = Guid.Empty;
newCustomer.Attributes.Remove("accountid");
proxy.Create(newCustomer);

A small note here. After you clone from the parent record and create a new customer, please remember to remove the primary key fields. Otherwise you would get an error like – Cannot insert duplicate key.

And voila! It worked.

A little bit of more searching and I came across this wonderful blog post. http://inogic.com/blog/2014/08/clone-records-in-dynamics-crm/. A greet post indeed like all other posts from Inogic. Wish I would have searched that before. Smile. It contains multiple ways to clone the record in Dynamics CRM.

 

P.S – The stuff I have mentioned here is only available in CRM on-premise. The Inogic blog gives you a very informative pointer to achieve this across all environments.

 

Hope this helps!

 

Advertisements

5 thoughts on “{knowhow} Clone an entity record programmatically in Microsoft Dynamics CRM using Clone method

  1. Very Nice blog ,
    but Microsoft.Xrm.Client does not exist any more at CRM 2016.
    Do You know what is its replaceable?

    • Hi,

      Thanks for reading my blog.

      You are right. Microsoft has made a decision to move everything to Microsoft.XrmTooling. However you can still use the 2015 SDK dll for 2016 projects.
      Also you can look forward to nuget packages and downloads in visual studio. it is still available there.

      Regards
      Debajit

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s