{dynamics crm clone record} Using Xrm.Utility.openEntityForm to clone all fields of one record to another in Dynamics CRM.

Looking at the title of the topic, you must be wondering what’s new in here. I think many of us by this time already know that we can clone one record of an entity to create another record easily using the  Xrm.Utility.openEntityForm method. For e.g suppose we have an account record and we have a functionality provided to clone the account record. The cloned record would contain the same values and the parent record.

To do this in CRM, all you need to do is the following.

var parameters = {};
parameters["name"] = "Test";
parameters["telephone1"] = "(425) 555-1234";
Xrm.Utility.openEntityForm("account", null, parameters);

Now comes the big question. Although its pretty simple to copy field values from one record to another using the above method, imagine of a situation where there are lot of fields on the form to clone. Even for that matter there can be situations where the field is not even in the form of the parent record but you would still need to copy it to the clone record. What’s the solution in that case?

Well as they say Old is Gold. Remember the _CreateFromId and _CreateFromType parameters that we used to clone a record in our early CRM 2011 days. You can use the same stuff in CRM 2013/ 2015 also. The following is the code to copy all the values from the parent account record to the cloned account record.

var accountId = Xrm.Page.data.entity.getId();

accountId = accountId.replace(‘{‘, ”).replace(‘}’, ”);

var parameters = {};

parameters["_CreateFromId"] = "{" + accountId + "}"; // Guid of the record from which values would be copied

parameters["_CreateFromType"] = "1"; // objecttypecode of the record

parameters["etc"] = "1"; // cloned entity objecttypecode

parameters["pagetype"] = "entityrecord";

Xrm.Utility.openEntityForm(Xrm.Page.data.entity.getEntityName(), null, parameters);

And voila! You are presented with a new form with all the values copied from the parent record.

Hope this helps!