【问题标题】:CRM 2011 Post Operation Plugin - Primary entity does not exist when attempting to save a referenced entityCRM 2011 Post Operation Plugin - 尝试保存引用实体时不存在主要实体
【发布时间】:2012-05-17 14:54:28
【问题描述】:

我正在尝试为现场 Dynamics CRM 2011 安装创建插件。

我已将插件注册如下:

  • 消息:创建
  • 主要实体:联系人
  • 执行阶段:手术后
  • 执行模式:同步
  • 执行顺序: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 工具分析插件并调试故障异常时,我收到以下错误:

Id = abbc7e0a-20a0-e111-a36e-005056860004 的联系人不存在。

我的理解是插件在尚未提交的 SQL 事务中执行。 CRM SDK 示例中的“FollowupPlugin”也创建了一个引用实体,它声明它需要异步注册,这也是有意义的,因为它允许 SQL 事务提交。

所以我想我的问题是,如何在同步插件中创建引用实体?

【问题讨论】:

    标签: dynamics-crm-2011


    【解决方案1】:

    查看SDK example for a very similar example - 我认为您需要在沙盒中注册。

    在创建消息上和异步模式下为帐户实体注册此插件。 或者,您可以在沙盒中的后期事件上注册插件

    【讨论】:

    • 非常感谢,更改为沙盒模式让我能够在同步插件中创建一个引用实体,我猜我在研究方面已经超载并错过了明显的文档。
    • 我有同样的问题,我不能使用沙盒模式,有什么替代方案吗??
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-12-23
    • 2018-06-08
    • 1970-01-01
    • 1970-01-01
    • 2012-04-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多