There are often such requirements, a document has too much to fill in the content, and sometimes associated with multiple sub-documents, customers do not want to fill in one, they want to copy the data from the existing documents, cloned into a new record. This article describes how to clone a record, including its child documents, to generate a new record.
It mainly uses the Microsoft.Xrm.Client.EntityExtensions.Clone method to clone data, and uses Organizationservicecontext to dynamically copy the data of the sub-document.
First add a clone button on the interface, add a New_clone field, click the button, the New_clone field is set to clone to trigger the plug-in, the plug-in to complete the copy of the data, and once again set the New_clone field to the new data id,new_ The onchange event of clone invokes Xrm.Utility.openEntityForm ("New_marketing_plan", Clonevalue) to open the new data.
Let's look at the implementation method:
1. js on the interface:
Clone Field Change Event
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);
Ten: }
11:}
Clone button Click event
function Clone () {
: var clone = Xrm.Page.getAttribute ("new_clone");
: Clone.setsubmitmode ("Always");
: clone.setvalue ("clone");
: Xrm.Page.data.entity.save ();
19:}
2. Main document replication:
true);
new New_marketing_plan () {Id = newmp.id};
4:guid newmpid = Guid.NewGuid ();
5:newmp.attributes.remove ("new_marketing_planid");
6:newmp.attributes.remove ("new_name");
New Optionsetvalue (1);
8:newmp.new_clone = "";
null;
10:newmp.id = Newmpid;
11:adminservice.create (NEWMP);
3. Copy the child table:
I have 9 sub-tables here, so I pulled out a way to make it easy to use, and if the sub-documents change, just 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";
in Entnames.split (', '))
3: {
4:clonerelatedentities (Adminservice, Newmpid, Entname, "New_marketing_planid", MP);
5:}
7:curent["new_clone"] = newmpid.tostring ();
8:adminservice.update (curent);
Private void Clonerelatedentities (Iorganizationservice adminservice, Guid Newentityid, String subentityname, String filterName, Entity parententity)
2: {
3: try
4: {
5: new Organizationservicecontext (Adminservice))
6: {
7: var ents = Svccontext.createquery (subentityname). Where (e = e[filtername] = = Parententity[filtername]). ToList ();
8: foreach (var in Ents)
9: {
£ º var newent = Microsoft.Xrm.Client.EntityExtensions.Clone (ENT);
One: new EntityReference (Subentityname, Newentityid);
: newent.id = Guid.NewGuid ();
: null;
: adminservice.create (Newent);
: }
: }
: }
: Catch (Exception ex)
: {
: thrownew invalidpluginexecutionexception (Methodbase.getcurrentmethod (). Name + "" + ex. Message);
: }
22:}
4. Precautions
- When I finished the unit Test, after registering the plugin, I reported the following error:
Could 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
Originally this client DLL on the server is not, we need to manually copy up. Copy from our native "SDK\Bin" to "Program Files\Microsoft Dynamics Crm\crmweb\bin" on the server.
- Click the Clone button again and report a mistake:
This workflow job is canceled because the workflow that started it included aninfinite loop. Correct the workflow logic and try again. For information about workflow logic, see Help
The original cycle of death, the solution is very simple, at the beginning of the plug-in Add the following code on the line:
if (context. Depth > 1)
{
Return
}
Dynamic CRM 2013 Learning Notes Series Rollup
Dynamic CRM 2013 Learning Notes (14) Copy/clone record