0

我寫具有以下配置插件:Prevalidation上創建插件

enter image description here

我只是想設置一個datetime場等於另一個datetime領域:

IPluginExecutionContext context = localContext.PluginExecutionContext; 
      IOrganizationService service = localContext.OrganizationService; 

      if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity) 
      { 
       // Obtain the target entity from the input parmameters. 
       Entity entity = (Entity)context.InputParameters["Target"]; 
       try 
       { 
        if (entity.LogicalName == "list" && entity.Attributes["gbs_lastusedonoriginal"] != null) 
        { 
         entity.Attributes["lastusedon"] = entity.Attributes["gbs_lastusedonoriginal"]; 
         service.Update(entity); 
        } 
       } 
       catch (FaultException ex) 
       { 
        throw new InvalidPluginExecutionException("An error occured in the plug-in.", ex); 
       } 

      } 

我得到的例外是:

未處理的Exc主器件接收: System.ServiceModel.FaultException`1 [[Microsoft.Xrm.Sdk.OrganizationServiceFault, Microsoft.Xrm.Sdk,版本= 6.0.0.0,文化=中性 公鑰= 31bf3856ad364e35]]:在 發生錯誤插件in.Detail:
-2147220891 OperationStatus SubErrorCode -2146233088 在插件發生錯誤。
2015-01-15T05:34:00.1772929Z

[PreValidationMarketingList.Plugins: PreValidationMarketingList.Plugins.PreValidateMarketingListCreate] [5454a088-749c-E411-b3df-6c3be5a83130:PreValidateMarketingListCreate]

進入 PreValidationMarketingList。 Plugins.PreValidateMarketingListCreate.Execute(), 相關性id:6d3ed105-f9c4-4006-9c80-08abd97c0140,發起用戶: 5e1b0493-d07b-E411-b592-f0921c199288 PreValidationMarketingList.Plugins.PreValidateMarketingListCreate是 FIRI納克爲實體:列表中,消息:創建,相關性id: 6d3ed105-f9c4-4006-9c80-08abd97c0140,發起用戶: 5e1b0493-d07b-E411-b592-f0921c199288退出 PreValidationMarketingList.Plugins.PreValidateMarketingListCreate.Execute(), 相關ID:6d3ed105-f9c4-4006-9c80-08abd97c0140,啓動用戶: 5e1b0493-d07b-E411-b592-f0921c199288

我在做什麼錯? 在crm 2013如何設置一個字段等於另一個字段,如果他們都是日期時間?

回答

6

您不應該在此插件中調用Update,因爲您尚未創建並保存您嘗試更新的記錄。

首先,將其移至預操作,而不是預驗證。這是一個不錯的選擇,但是前期操作確實是合適的地方,因爲創建list時不需要設置lastusedon進行驗證。

我返工你的代碼做一些額外的檢查:

 IPluginExecutionContext context = localContext.PluginExecutionContext; 
     IOrganizationService service = localContext.OrganizationService; 

     if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity) 
     { 
      // Obtain the target entity from the input parmameters. 
      Entity entity = (Entity)context.InputParameters["Target"]; 
      try 
      { 
       if (entity.LogicalName == "list" && entity.Attributes.Contains("gbs_lastusedonoriginal") && entity["gbs_lastusedonoriginal"] != null) 
       { 
        if (entity.Attributes.Contains("lastusedon")) 
         entity.Attributes["lastusedon"] = entity.Attributes["gbs_lastusedonoriginal"]; 
        else entity.Attributes.Add("lastusedon", entity.Attributes["gbs_lastusedonoriginal"];       
       } 
      } 
      catch (FaultException ex) 
      { 
       throw new InvalidPluginExecutionException("An error occured in the plug-in.", ex); 
      } 

     } 
+1

很好的答案,雖然我認爲你應該更明確地解釋爲什麼即使你已經刪除了更新呼叫,該字段仍然得到更新。 – Zach

+0

查看我的帖子和鏈接到MSDN頁面 –

+0

非常感謝!它不足以做到這一點:實體[「gbs_lastusedonoriginal」]!= null而不是這兩個:entity.Attributes.Contains(「gbs_lastusedonoriginal」)&& entity [「gbs_lastusedonoriginal」]!= null –

2

檢查MSDN this鏈接。在事件前,記錄還沒有保存在SQL數據庫中。您可以從InputParameters修改實體對象。在事件前插件之後,將使用修改後的屬性創建記錄。

預驗證和預操作之間的主要區別在於預操作階段在數據庫事務中執行,而預驗證不執行。有關更多信息,請參見MSDN