Cloning Records in Microsoft Dynamics CRM 2011

Roshan Mehta, 12 January 2012

We were recently asked to build a record cloning system in Microsoft Dynamics CRM 2011 where the customer would be able to click on a Clone Contract button on a custom “Funding Contract” entity form ribbon and have an identical record automatically pop open. There are multiple solutions we could use to build this functionality, but we decided to stick with out-of-the-box customization techniques and limited coding.

 Cloning Records in Microsoft Dynamics CRM 2011

So what are the options for building the cloning system? We could use a plug-in which will copy fields from the original Funding Contract and automatically create a new record using the same data. The disadvantage of this approach is that the customer would either have to come back to us in order to map additional fields, or have some .NET knowledge if they wish to modify the plug-in themselves.

Another approach is to use JScript and pass the data to be cloned to a new Funding Contract using URL Addressable Forms. This also has a major disadvantage as it depends on the amount of data being passed to the new form. For example, if the Funding Contract has a Description field which holds 10,000 characters, the URL will be too long and the cloned record will never be displayed.

We decided to use a combination of entity relationships, mappings, ribbon customizations, JScript, and URL Addressable Forms to handle the record cloning for us. Firstly, we create a 1:N self-referential relationship for the Funding Contract entity. This means that a Funding Contract can have multiple child Funding Contracts.

Next, we define the fields to be cloned by using relationship mappings. Because the relationship is self-referential, we can always be certain that the source and target fields defined in each mapping will not cause any issues because the data types and field lengths will always be the same.

 Cloning Records in Microsoft Dynamics CRM 2011

After we define the mappings and publish the customizations, we can open up a Funding Contract and create a new related Funding Contract from the left-hand menu. When the new record form is displayed, we can confirm that all of the necessary fields have been mapped. Copy the URL of the new record as we will need to use this in our JScript web resource.

http://<server>/<org>/main.aspx?etc=10019&extraqs=%3f_CreateFromId%3d%257b83D303DE-6B03-E111-9EE0-00155D031A24%257d%26_CreateFromType%3d10019%26etc%3d10019&pagetype=entityrecord

The etc and CreateFromID parameters need to be dynamic so the cloning function can work across any organization. To achieve this, our ribbon button definition needs to pass in the entity type code and the ID of the original Funding Contract using the <CrmParameter> tag. Now that we have a dynamically generated URL, we can use the window.open JScript command to pop open our cloned Funding Contract.

To sum up, we have built a simple and effective record cloning functionality using out-of-the-box customization techniques. If the customer wants to clone additional fields, they can simply create mappings in CRM which will automatically copy the data to the new Funding Contract. There is no need for additional code as all of the field cloning is handled using out-of-the-box customization features.