批量删除不会激发CRM动态中的插件
本文关键字:动态 CRM 插件 删除 | 更新日期: 2023-09-27 18:01:10
我想在Microsoft CRM dynamics 2013中从批量删除中触发一个插件。我创建了一个自定义实体,我的插件指向这个自定义实体的删除操作。我配置插件注册如下:
Message : "delete"
Primary entity : "my custom entity",
Run in User's Context : "Calling User",
Event pipeline : "Pre operation",
Execution mode : "synchronous"
批量删除过程没有启动插件。它没有抛出错误
然而,插件可以通过手动删除操作触发。但我想从任何系统事件中按计划触发插件(批量删除(
请帮我解决这个问题。
这是我的代码:
Microsoft.Xrm.Sdk.IPluginExecutionContext context = (Microsoft.Xrm.Sdk.IPluginExecutionContext)
serviceProvider.GetService(typeof(Microsoft.Xrm.Sdk.IPluginExecutionContext));
if (context.Depth == 1)
{
IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService service = serviceFactory.CreateOrganizationService(context.InitiatingUserId);
EntityReference entity = (EntityReference)context.InputParameters["Target"];
DateTime dtToday = DateTime.Now;
DateTime toDate = dtToday.AddMonths(-1);
QueryExpression qe = new QueryExpression("contact");
qe.ColumnSet = new ColumnSet(new String[] { "birthdate", "firstname", "address1_name" ,"new_lastapptdate" });
qe.Criteria.AddCondition("new_lastapptdate", ConditionOperator.LessEqual, toDate);
EntityCollection collection = service.RetrieveMultiple(qe);
foreach (Entity contact in collection.Entities)
{
}
try
{
var factory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
Entity Ageentity = new Entity("new_agecalc");
Ageentity.Attributes.Add("new_name", "AgecalcTest");
service.Create(Ageentity);
}
catch (Exception ex)
{
throw new InvalidPluginExecutionException("An error ", ex);
}
}
}
在您的代码中,您正在检查上下文深度是否等于1。在批量删除过程中,它已经是作业的一部分,因此深度将大于1。如果你删除了这个检查,你的插件应该可以工作。