我正在尝试为现场Dynamics CRM 2011安装创建一个插件。
我已按如下方式注册了该插件:
插件代码如下:
public void Execute(IServiceProvider serviceProvider)
{
var pluginExecContext = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
var orgServiceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
var orgService = orgServiceFactory.CreateOrganizationService(pluginExecContext.UserId);
var orgServiceContext = new OrganizationServiceContext(orgService);
var tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
if (pluginExecContext.InputParameters.Contains("Target") &&
pluginExecContext.InputParameters["Target"] is Entity)
{
var target = (Entity)pluginExecContext.InputParameters["Target"];
if (target.LogicalName != Contact.EntityLogicalName)
return;
try
{
var customerServicesUser = orgServiceContext.CreateQuery(SystemUser.EntityLogicalName)
.Where(x => (string)x["fullname"] == "Customer Services").FirstOrDefault();
if (customerServicesUser == null)
throw new InvalidPluginExecutionException("No Customer Services user exists.");
var sendEmail = new cdi_sendemail
{
cdi_fromrecordowner = false,
cdi_contactid = new EntityReference(Contact.EntityLogicalName, pluginExecContext.PrimaryEntityId),
cdi_fromid = new EntityReference(SystemUser.EntityLogicalName, customerServicesUser.Id)
};
tracingService.Trace("PostContactCreate plug-in: Creating the cdi_sendemail entity.");
orgService.Create(sendEmail);
}
catch (FaultException<OrganizationServiceFault> ex)
{
throw new InvalidPluginExecutionException("An error occurred in the PostContactCreate plug-in.", ex);
}
catch (Exception ex)
{
tracingService.Trace("PostContactCreate plug-in: {0}", ex.ToString());
throw;
}
}
}
当我使用pluginregistration工具分析插件并调试错误异常时,我收到以下错误:
与Id = abbc7e0a-20a0-e111-a36e-005056860004的联系不存在。
我有点理解,因为插件正在一个尚未提交的SQL事务中执行。 CRM SDK示例中的“FollowupPlugin”也创建了一个引用的实体,声明它需要异步注册,这也是有意义的,因为它允许SQL事务提交。
所以我想我的问题是,如何在同步插件中创建引用的实体?
答案 0 :(得分:3)
查看SDK example for a very similar example - 我认为您需要在沙箱中注册。
在创建消息和异步模式下为帐户实体注册此插件。 或者,您可以在沙盒中的事件后注册插件。