Dynamic CRM 2013学习笔记(十四)复制/克隆记录

经常有这样的需求,一个单据上有太多要填写的内容,有时还关联多个子单据,客户不想一个一个地填写,他们想从已有的单据上复制数据,克隆成一条新的记录。本文将介绍如何克隆一条记录,包括它的子单据以生成一条新的记录。

主要用到Microsoft.Xrm.Client.EntityExtensions.Clone方法来克隆数据,以及用OrganizationServiceContext来动态复制子单据的数据。

首先在界面上新加一个Clone的按钮,加一个new_clone的字段;点击按钮时,把new_clone字段设为clone以触发插件,插件里完成数据的复制工作,并再次把new_clone字段设成新数据的id,new_clone的onchange事件会调用Xrm.Utility.openEntityForm("new_marketing_plan", cloneValue);来打开新的数据。

下面看下实现方法:

1. 界面上js:

  function openNewEntity() {
      var clone = Xrm.Page.getAttribute("new_clone");
      var cloneValue = clone.getValue();
      if (cloneValue != "clone" && cloneValue != "") {
          clone.setSubmitMode("always");
          clone.setValue("");
          Xrm.Page.data.entity.save();
          Xrm.Utility.openEntityForm("new_marketing_plan", cloneValue);
      }
  }   //clone button click event   function clone() {
      var clone = Xrm.Page.getAttribute("new_clone");
      clone.setSubmitMode("always");
      clone.setValue("clone");
      Xrm.Page.data.entity.save();
  }

2. 主单据复制:

new_marketing_plan newMP = (new_marketing_plan)Microsoft.Xrm.Client.EntityExtensions.Clone(curEnt, true);
new_marketing_plan mp = new new_marketing_plan() { Id = newMP.Id };
Guid newMPid = Guid.NewGuid();
newMP.Attributes.Remove("new_marketing_planid");
newMP.Attributes.Remove("new_name");
newMP.new_approval_status = new OptionSetValue(1);
newMP.new_clone = "";
newMP.EntityState = null;
newMP.Id = newMPid;
adminService.Create(newMP);

3. 子表复制:

我这里有9个子表,所以抽出了一个方法以方便使用, 以后要是子单据有变化,只用改下这里的entNames就行了。

  //clone field change event
 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";
 foreach (string entName in entNames.Split(','))
 {
   CloneRelatedEntities(adminService, newMPid, entName, "new_marketing_planid", mp);
 }  curEnt["new_clone"] = newMPid.ToString();
 adminService.Update(curEnt);
  private void CloneRelatedEntities(IOrganizationService adminService, Guid newEntityId, string subEntityName, string filterName, Entity parentEntity)
{  
        using (OrganizationServiceContext svcContext = new OrganizationServiceContext(adminService))
        {
            var ents = svcContext.CreateQuery(subEntityName).Where(e => e[filterName] == parentEntity[filterName]).ToList();
            foreach (var ent in ents)
            {
                 var newEnt = Microsoft.Xrm.Client.EntityExtensions.Clone(ent);
                 newEnt.Attributes[filterName] = new EntityReference(subEntityName, newEntityId);
                 newEnt.Id = Guid.NewGuid();
                 newEnt.EntityState = null;
                 adminService.Create(newEnt);
             }
         }
 }

4. 注意事项

  • 当我完成Unit Test,注册完插件后,报了下面的错:

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

原来这个Client dll在服务器上是没有的,需要我们手动copy上去。从我们本机的“SDK\Bin”里copy到服务器上的“program files\Microsoft Dynamics CRM\CRMWeb\bin”下即可。

  • 再点击Clone按钮,又报了一个错:

This workflow job was canceled because the workflow that started it included an infinite loop. Correct the workflow logic and try again. For information about workflow logic, see Help

原来出现死循环了,解决方法很简单,在插件的开始处加上下面代码就行了:

if (context.Depth > 1)
            {

return;

}

Dynamic CRM 2013学习笔记 系列汇总

上一篇:CRM 2013 系统设置新功能二:Entity images 图像字段


下一篇:Sublime Text3自动保存的功能(失去焦点自动保存)