[英]how to retrieve entity on update in dynamics crm 2011
我是CRM的新手,正在尝试编写插件。
它应该很简单,但事实并非如此。
由于某种原因,我无法检索实体。 我肯定知道它存在于数据库中并且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);
}
}
}
不要忘记,在update消息中,您只能访问要对其进行更新(更改)的文件...您可以按以下方法编写,以跟踪您的代码(行到行)
public static void LogFile(string log)
{
try
{
TextWriter tw = new StreamWriter(@"C:\Inetpub\wwwroot\inventorylog_" + Guid.NewGuid() + ".txt");
tw.Write(log);
tw.Close();
}
catch
{
}
}
成功的
好吧,您可以给我们更多有关它的信息,例如是否抛出异常? 我的猜测是,您收到以下错误:“ ID中为“ yourentityid”的“ yourentity”在数据库中不存在”,如果是这样,则您可能正在尝试检索在操作前管道上创建的记录。 无论如何,请帮助我们帮助您提供更多信息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.