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學習筆記 系列匯總


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM