CRM 插件 PreImage 不更新相关实体
本文关键字:实体 更新 插件 PreImage CRM | 更新日期: 2023-09-27 17:56:13
我对开发相当陌生,但在为Dynamics CRM创建插件时遇到了问题。当字段 primarycontactid 更新为其他内容时,该插件应该在联系人实体上将 parentcustomerid 字段
呈现为空时,当它链接到的客户实体更新为空时,要么在链接到另一个联系人时为空。使用我编写的代码,当前没有抛出任何错误,并且代码执行成功,但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"];
}