latest Post

Clone Functionality with custom actions

Ever since version for 4 of Dynamics CRM I have been waiting for them to release out of the box functionality that allows the user to clone records. However, up to now we are still waiting for this which surprises me because I have worked on a number of projects where the client wants to be able to clone Opportunities, Quote's or Sales Orders.

Looking on the forums lots of people suggest using an OOB workflow to to create a new record and map the columns. However, this does not work because we cannot map the line items associated with the records in an OOB workflow.

In the past I have accomplished this with a custom workflow however, since the introduction of custom actions in CRM 2015 I now achieve the desired functionality using a custom action which is fired by JavaScript which in turn triggers a plugin to create the record.

1.) Create custom action
First of all we need to create the custom action. Here I have named it "CloneRecord" and configured it as a global entity meaning it is reusable and can be called from any entity in the system. I have then setup two parameters a input parameter named sourceRecord of type Entity Reference (This is the record we are going to clone) and an output parameter clonedRecord of type entity reference (the returned cloned record).

2.) Write some JavaScript to trigger the custom action
The below function triggers the above custom action. First of all we setup our boiler plate code. We get the CRM URL, Action name and ID of the record to clone. We then setup the HTTPRequest() object passing in the entity we want to clone, the id, and organisation url. Note here I have hard coded "salesorder" type we could easily have passed this as a parameter to make this code reusable.

 In side our HTTPRequest we then listen on the readystateChange event for a 200 OK to be returned. Once returned we can then read the response from the plugin and open the new record in  a new browser window so the user can begin working on it.

3.) Write custom plugin to fire on custom action
The next step is to create and register a plugin to fire on the custom action. This blog post is not going to go into how to create and register plugins but if your new to this you can follow Microsoft's walk through guide here.
As you can see from the above code, I simply read in the input parameter and create a new output parameter and copy the attributes between he source record and the cloned record in the "CloneData" function. Note: The use of generics to make this reusable for other entity types. I have not hard coded any attribute names. The generics used in the "Clone Data" function was taken from a brilliant blog post on the website.

Once done we then used the CRM SDK to create the parent record. We then queried CRM to get any related child items for the parent using the query expression syntax. Note, I have hard coded the child entity name in the query, this was due to time constraints however, to make this fully reusable I need to look into a way to query all child records for the parent using generics.

Once the child items are returned I then loop through each item in the list and call the "cloneData" function again to left and right the child attributes from the source record to the cloned record. Once done we simply create the Entity reference between the child and parent and use the SDK create method to create the child records.

 4.) Add button to the entity you want to clone ribbon which will fire the above java script.
Finally the last step to make the solution user friendly is to add a "clone button" to the form ribbon of the entity you wish to clone. Now this is up to you if you want to modify the sitemap XML manually or use the brilliant Ribbon Workbench editor from the guys at develop1. Their website has some tutorials on how to use it.

All I did was drop a new button onto the ribbon and set it up to call the above JavaScript function which fires the custom action. In turn this will then fire the plugin and once complete return the data to the JavaScript event which in turn will open the cloned record in a new window.

All in all I personally think this a nice solution and with a bit more time can be made fully generic to be re used for any entity in the system. I know there are some 3rd party tools now in the market place that do similar things but where is the fun in that. 

Also a big thanks to your blog post give me the incentive for this but the solution was adapted for my needs.

Recommended Posts × +


  1. Vendemos alguns produtos de diferentes caixas personalizadas.é muito útil e de preço muito baixo, visite este site obrigado e compartilhe este post com seus amigos. clonar Instagram