2013-05-01 99 views
0

我是CRM新手,試圖編寫一個插件。如何檢索動態crm 2011中的實體更新

它應該很簡單,但事實並非如此。

由於某種原因,我無法檢索實體。 我知道它確實存在於數據庫中,並且ID是正確的。

下面的代碼....

有沒有人有任何想法,爲什麼它不工作? 感謝

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using Microsoft.Xrm.Sdk; 
using System.ServiceModel; 
using Microsoft.Xrm.Sdk.Query; 
using System.Collections; 

namespace Copy_field 
{ 
    public class Copy_field : IPlugin 
    { 
     /// <summary> 
     /// A plugin copyies fields from Contact Entity to Case Entity. This allows display 
     /// information about the client on case Entity and change it directly from Case Entity 
     /// </summary> 
     /// <remarks>Register this plug-in on the Create case, update case and update of contact 
     /// </remarks> 
     /// 

     public void Execute(IServiceProvider serviceProvider) 
     { 
      IPluginExecutionContext context = (IPluginExecutionContext) serviceProvider.GetService(typeof(IPluginExecutionContext)); 

      ITracingService tracer = (ITracingService)serviceProvider.GetService(typeof(ITracingService)); 
      IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory)); 
      IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId); 

      Entity entity; 

      if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity) 
      { 

       entity = (Entity)context.InputParameters["Target"]; 
       if (entity.LogicalName != "incident") { return; } 
      } 
      else 
      { 
       return; 
      } 
     // tracer.Trace("1. THIS IS an/a: " + entity.LogicalName); 
      // tracer.Trace("2. Id: " + entity.Id); 
    //   tracer.Trace("2.2 output parametersId: " + context.OutputParameters["id"].ToString()); 

      // if record exists - retrieve the entity 

      /////////////////////////////////////////////////////////////////// 
     ColumnSet cols = new ColumnSet(true); 
     Entity yahhooo = service.Retrieve(entity.LogicalName, entity.Id, cols); 


     } 
    } 
} 
+0

你怎麼知道它不工作? – 2013-05-01 07:39:17

+0

是的,你的代碼看起來像定義。所以,JamesWood在詢問中是正確的,你怎麼知道它不起作用?你的代碼沒有被解僱嗎?它發射了嗎?你進入if區塊嗎? 「entity.Id」和「entity.LogicalName」的值是什麼?你還能告訴我們什麼? ;) – 2013-05-02 13:37:32

回答

0

嗯,你可以給我們多一點關於它的像,是它拋出任何異常信息?我的猜測是,您收到以下錯誤: 「數據庫中不存在id爲」yourentityid「的」yourentity「,如果是這樣,您可能試圖檢索Pre-Operation管道上創建的記錄。 無論如何,幫助我們幫助您向我們提供更多信息。

1

不要忘了,在更新消息,你只給您更新申請的(其他城市)吧... 訪問您可以按照下面的方法寫入跟蹤代碼(線上到線下)

public static void LogFile(string log) 
    { 
     try 
     { 
      TextWriter tw = new StreamWriter(@"C:\Inetpub\wwwroot\inventorylog_" + Guid.NewGuid() + ".txt"); 
      tw.Write(log); 
      tw.Close(); 
     } 
     catch 
     { 
     } 
    } 

成功

+0

這看起來像一個通用的調試技術。它如何幫助解決問題? – 2014-02-26 12:12:09

+0

無論條件代碼是否被執行,都可以使用。要意識到這是目標 – 2014-03-01 05:28:47

相關問題