After writing few blogs on integrations with dynamics CRM , i thaught of sharing something i achieved for one of my client requirement. A clone button can be used to reduce data inputs by user and do minor changes to the records to create another record.It can be used for any of the entity whereas i have used it for Leads in sales module for lead cloning.
This will require basic knowledge of javascript.
1. function leadClone(executionContext) {
2. //debugger;
3. "use strict";
4. var entityFormOptions = {};
5. //Entity Form Options
6. entityFormOptions["entityName"] = "lead"; // Replace the entity name as per you requirement
7. entityFormOptions["openInNewWindow"] = true;
8. var formParameters = {};
9. executionContext.data.entity.attributes.forEach(
10. function (attribute, index) {
11. var attributeName = attribute.getName();
12. var attributetype = attribute.getAttributeType();
13. var attrvalue = attribute.getValue();
14. if (
15. attributeName === 'ATT1' || // The attributes value which you want to pass
16. attributeName === 'ATT2 ' ||
17. attributeName === 'ATT3'
18.
19. ) {
20. if (attributetype === "lookup") {
21. if (attrvalue !== null) {
22. if (attrvalue[0].id !== null) {
23. var regObj = {};
24. regObj.entityType = attrvalue[0].entityType;
25. regObj.name = attrvalue[0].entityType.name;
26. regObj.id = attrvalue[0].id;
27. formParameters[attributeName] = attrvalue;
28. }
29. }
30. }
31.
32. if (attributetype === "boolean") {
33. formParameters[attributeName] = attrvalue;
34. }
35. if (attributetype === 'datetime') {
36. formParameters[attributeName] = attrvalue.toDateString();
37. }
38. if (attributetype === 'optionset') {
39. formParameters[attributeName] = attrvalue;
40. }
41. if (attributetype === 'string') {
42. formParameters[attributeName] = attrvalue;
43. }
44.
45. }
46.
47. });
48. Xrm.Navigation.openForm(entityFormOptions, formParameters);
49. }
The above code needs to be registered on the ribbon button click of the clone button.
Understanding the code
Lets discuss some features used in above code.
EntityFormOptions- Entity form options for opening the form. The object contains the following attributes:entityName: String. Logical name of the entity to display the form for.entityId: (Optional) String. ID of the entity record to display the form for.formId: (Optional) String. ID of the form instance to be displayed.cmdbar: (Optional) Boolean. Indicates whether to display the command bar. If you do not specify this parameter, the command bar is displayed by default.createFromEntity: (Optional) Lookup. Designates a record that will provide default values based on mapped attribute values. The lookup object has the following String properties: entityType, id, and name (optional).height: (Optional) Number. Height of the form window to be displayed in pixels.navbar: (Optional) String. Controls whether the navigation bar is displayed and whether application navigation is available using the areas and subareas defined in the sitemap. Valid vlaues are: “on”, “off”, or “entity”.on: The navigation bar is displayed. This is the default behavior if the navbar parameter is not used.off: The navigation bar is not displayed. People can navigate using other user interface elements or the back and forward buttons.entity: On an entity form, only the navigation options for related entities are available. After navigating to a related entity, a back button is displayed in the navigation bar to allow returning to the original record.openInNewWindow: (Optional) Boolean. Indicates whether to display form in a new window.
The above code uses Navigation utility of CRM to open create form of the entity.
On line 4 we are defining entityoptions object and passing entity name and windows options (line 6,7).
After setting the entity level parameters we will define formparameters. Formparameters are used to set attribute value in the preceding forms.In the above code we are fetching all the attributes details of the current form in a foreach loop maintaining a seperate condition for the datatypes available in CRM.
As you can see on line 15 I have implemented a condition where i need to add which all attributes needs to be cloned . You can remove that condition but remember it will clone owner value and createdon, modified on etc which might create problems.
You can use this piece of code on change of field, button click, on save or any other triggers suitable for your requirement.
So this was a quick cool tip to clone records without setting hardcoded values in the code.
Hope this helps you , Thanks for reading .
You can connect me on LinkedIN

This is quite well thought out. May I pose a question? Because..I know more points that support this.
LikeLike
So, I followed a link to your blog and I enjoyed this post a whole lot. . Where might I find out more?
LikeLike