2013-05-01 54 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