在Microsoft Dynamics 365中,我想删除一个帐户记录,但是要删除处于活动状态的任何子联系人。处于非活动状态的联系人应正常删除。我该如何使用插件来实现这一目标?
答案 0 :(得分:1)
不幸的是,Dynamics不允许我们更改“帐户到联系人1-N”关系中的级联删除,这意味着每次删除帐户时,与该帐户相关的所有联系人都会被删除。此关系也无法删除。
编辑:将下面的插件注册为 PreValidation 删除,它将生效(出于取消关联子记录的目的,请致谢@Sxntk)
public class AccountPreDeletePlugin : IPlugin
{
public void Execute(IServiceProvider serviceProvider)
{
var tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
var pluginExecutionContext = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
var organizationServiceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
var organizationService = organizationServiceFactory.CreateOrganizationService(pluginExecutionContext.UserId);
if (pluginExecutionContext.InputParameters.Contains("Target") && pluginExecutionContext.InputParameters["Target"] is EntityReference entityReference)
{
var query = new QueryExpression("contact") { NoLock = true };
query.Criteria.AddCondition("parentcustomerid", ConditionOperator.Equal, entityReference.Id);
query.Criteria.AddCondition("statecode", ConditionOperator.Equal, 0);
var entityCollection = organizationService.RetrieveMultiple(query);
if (entityCollection?.Entities?.Count > 0)
{
foreach (var entity in entityCollection.Entities)
{
entity["parentcustomerid"] = null;
organizationService.Update(entity);
}
}
}
}
}