我是 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);
}
}
}