There is often such a requirement that a document contains too many content to be filled in, and sometimes multiple sub-documents are associated. Customers do not want to fill in the documents one by one. They want to copy data from existing documents, create a new record. This article describes how to clone a record, including its sub-documents, to generate a new record.
Microsoft. xrm. Client. entityextensions. Clone is used to clone data, and organizationservicecontext is used to dynamically copy data of sub-documents.
Add a new clone button on the interface and a new_clone field. When you click the button, set the new_clone field to clone to trigger the plug-in. The plug-in completes data replication, set the new_clone field to the ID of the new data again. The onchange event of new_clone will call xrm. utility. openentityform ("new_marketing_plan", clonevalue); to open new data.
The implementation method is as follows:
1. js on the interface:
1: //clone field change event
2: function openNewEntity() {
3: var clone = Xrm.Page.getAttribute("new_clone");
4: var cloneValue = clone.getValue();
5: if (cloneValue != "clone" && cloneValue != "") {
6: clone.setSubmitMode("always");
7: clone.setValue("");
8: Xrm.Page.data.entity.save();
9: Xrm.Utility.openEntityForm("new_marketing_plan", cloneValue);
10: }
11: }
12:
13: //clone button click event
14: function clone() {
15: var clone = Xrm.Page.getAttribute("new_clone");
16: clone.setSubmitMode("always");
17: clone.setValue("clone");
18: Xrm.Page.data.entity.save();
19: }
2. Master Document Copy:
1: new_marketing_plan newMP = (new_marketing_plan)Microsoft.Xrm.Client.EntityExtensions.Clone(curEnt, true);
2: new_marketing_plan mp = new new_marketing_plan() { Id = newMP.Id };
3:
4: Guid newMPid = Guid.NewGuid();
5: newMP.Attributes.Remove("new_marketing_planid");
6: newMP.Attributes.Remove("new_name");
7: newMP.new_approval_status = new OptionSetValue(1);
8: newMP.new_clone = "";
9: newMP.EntityState = null;
10: newMP.Id = newMPid;
11: adminService.Create(newMP);
3. subtable replication:
I have nine sub-tables, so I have extracted a method for ease of use. If the sub-documents change in the future, I just need to change the entnames here.
1: string entNames = "new_print_plan,new_radio_plan,new_bill_board,new_tv_plan,new_btl_posm,new_btl_poe_fixed,new_promotion_girls,new_promotion_events,new_digital_plan";
2: foreach (string entName in entNames.Split(‘,‘))
3: {
4: CloneRelatedEntities(adminService, newMPid, entName, "new_marketing_planid", mp);
5: }
6:
7: curEnt["new_clone"] = newMPid.ToString();
8: adminService.Update(curEnt);
1: private void CloneRelatedEntities(IOrganizationService adminService, Guid newEntityId, string subEntityName, string filterName, Entity parentEntity)
2: {
3: try
4: {
5: using (OrganizationServiceContext svcContext = new OrganizationServiceContext(adminService))
6: {
7: var ents = svcContext.CreateQuery(subEntityName).Where(e => e[filterName] == parentEntity[filterName]).ToList();
8: foreach (var ent in ents)
9: {
10: var newEnt = Microsoft.Xrm.Client.EntityExtensions.Clone(ent);
11: newEnt.Attributes[filterName] = new EntityReference(subEntityName, newEntityId);
12: newEnt.Id = Guid.NewGuid();
13: newEnt.EntityState = null;
14: adminService.Create(newEnt);
15: }
16: }
17: }
18: catch (Exception ex)
19: {
20: throw new InvalidPluginExecutionException(MethodBase.GetCurrentMethod().Name + " " + ex.Message);
21: }
22: }
4. Notes
- After I complete the unit test and register the plug-in, the following error is reported:
Cocould not load file or assembly 'Microsoft. xrm. Client, version = 6.0.0.0, culture = neutral, publickeytoken = 31bf3856ad364e35 'or one of its Dependencies. The system cannot find the file specified
It turns out that this client DLL does not exist on the server and needs to be manually copied. Copy from the "SDK \ bin" on our local machine to the "Program Files \ Microsoft Dynamics CRM \ crmweb \ bin" on the server.
- Click the clone button and an error is reported:
This workflow job was canceled because the workflow that started it has dedInfinite Loop. Correct the workflow logic and try again. For information about workflow logic, see help
It turns out that there is an endless loop, and the solution is simple. Add the following code at the beginning of the plug-in:
If (context. Depth> 1)
{
Return;
}
Dynamic CRM 2013 learning notes Series
Dynamic CRM 2013 study notes (14) copy/clone records