C# CRM插件前映像未更新相关实体

C# CRM插件前映像未更新相关实体,c#,plugins,dynamics-crm,C#,Plugins,Dynamics Crm,我对开发相当陌生,但在为Dynamics CRM创建插件时遇到了一个问题。当链接到的帐户实体在primarycontactid字段被更新为其他字段(链接到另一个联系人时为null)时被更新,插件应该在contact实体上呈现parentcustomerid字段null 对于我目前编写的代码,不会抛出任何错误,代码执行成功,但parentcustomerid字段仍然包含它应该删除链接时链接的帐户 以下是我目前拥有的代码: public void Execute(IServiceProvid

我对开发相当陌生,但在为Dynamics CRM创建插件时遇到了一个问题。当链接到的帐户实体在primarycontactid字段被更新为其他字段(链接到另一个联系人时为null)时被更新,插件应该在contact实体上呈现parentcustomerid字段null

对于我目前编写的代码,不会抛出任何错误,代码执行成功,但parentcustomerid字段仍然包含它应该删除链接时链接的帐户

以下是我目前拥有的代码:

    public void Execute(IServiceProvider serviceProvider)
    {       
            ITracingService tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));


            //Obtain the execution context
            IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));

            //obtain organizational services
            IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
            IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);

            EntityReference prePCID;
            Entity PreImage;
            // The InputParameters collection contains all the data passed in the message request.
            if (context.InputParameters.Contains("Target") &&
                context.InputParameters["Target"] is Entity)
            {
                // Obtain the image entity from the Pre Entity Images.
                tracingService.Trace
                    ("trace1: Getting the target entity from Input Parameters.");
                PreImage = (Entity)context.InputParameters["Target"];

                // Verify that the target entity represents an account.
                // If not, this plug-in was not registered correctly.
                tracingService.Trace
                   ("trace2: Verifying that the target entity represents a account.");
                if (PreImage.LogicalName == "account")
                {
                    if (PreImage.Attributes.Contains("primarycontactid"))
                    {
                        tracingService.Trace
                            ("trace3: Setting the primary contact id in the prePCID.");
                    //prePCID = (EntityReference)PreImage.Attributes["primarycontactid"];
                    prePCID = (EntityReference)PreImage["primarycontactid"];

                        tracingService.Trace
                            ("trace4: Primary Contact Name: " + prePCID.Name + " Creating a variable that stores the contact using the prePCID.");
                        Entity contactToRemoveReference = service.Retrieve("contact", prePCID.Id, new ColumnSet("parentcustomerid")) as Entity;
                        tracingService.Trace
                            ("trace5: Removes the id: " + prePCID.Id + " of the parentcustomerid.");
                        contactToRemoveReference.Attributes["parentcustomerid"] = null;

                        service.Update(contactToRemoveReference);
                        tracingService.Trace
                            ("trace6: Execution Successful.");
                    }
                } 

        }
}


如果有人能帮我解决这个问题,那将非常感谢。

您正在输入参数中从主实体填充实体变量PreImage。 我认为您应该从预映像中获取它,例如:

Entity PreImage;
if (context.PreEntityImages.Contains("yourPreImageName") && context.PreEntityImages["yourPreImageName"] != null)
{
    PreImage = context.PreEntityImages["yourPreImageName"];
}

您正在从输入参数中的主实体填充实体变量PreImage。 我认为您应该从预映像中获取它,例如:

Entity PreImage;
if (context.PreEntityImages.Contains("yourPreImageName") && context.PreEntityImages["yourPreImageName"] != null)
{
    PreImage = context.PreEntityImages["yourPreImageName"];
}

你的插件是如何注册的?消息是update,主要实体是account。当我注册该步骤时,它被设置为预操作。预映像被过滤为primarycontactid,就像stepHow一样。您的插件注册了吗?消息是更新,主要实体是帐户。当我注册该步骤时,它被设置为pre-Op。与步骤一样,pre-Image被过滤为primarycontactid