CRM 2011 Post Operation插件-尝试保存引用的实体时不存在主要实体 [英] CRM 2011 Post Operation Plugin - Primary entity does not exist when attempting to save a referenced entity
问题描述
我正在尝试为现场Dynamics CRM 2011安装创建插件.
I am attempting to create a plugin for an onsite Dynamics CRM 2011 installation.
我已经按照以下步骤注册了插件:
I have registered the plugin as follows:
- 消息:创建
- 主要实体:联系人
- 执行阶段:手术后
- 执行模式:同步
- 执行顺序:1
插件代码如下:
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工具分析插件并调试错误异常时,出现以下错误:
When I profile the plugin using the pluginregistration tool and debug the fault exception I get the following error:
与ID = abbc7e0a-20a0-e111-a36e-005056860004的联系不存在.
Contact With Id = abbc7e0a-20a0-e111-a36e-005056860004 Does Not Exist.
我有点理解,因为插件正在尚未提交的SQL事务中执行.CRM SDK示例中的"FollowupPlugin"还创建了一个引用的实体,指出它需要异步注册,这也很有意义,因为它允许提交SQL事务.
Which I sort of understand as the plugin is executing within a SQL transaction that has yet to commit. The "FollowupPlugin" within the CRM SDK samples, which also creates a referenced entity, states that it needs to be registered asynchronously, which would also make sense as that allows the SQL transaction to commit.
所以我想我的问题是,如何在同步插件中创建引用的实体?
So I guess my question is, how do you create a referenced entity in a synchronous plugin?
推荐答案
查看 SDK示例中的一个非常相似的示例-我认为您需要在沙箱中注册.
Looking at the SDK example for a very similar example - I think you need to register in the sandbox.
在创建"消息上并以异步模式为帐户实体注册此插件.或者,您可以在沙箱中的事件后注册插件.
这篇关于CRM 2011 Post Operation插件-尝试保存引用的实体时不存在主要实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!