批量删除不会在CRM动态中触发插件

时间:2014-05-01 08:27:44

标签: c# plugins dynamics-crm-2011 dynamics-crm-online dynamics-crm-2013

我想在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 个答案:

答案 0 :(得分:2)

在您的代码中,您正在检查上下文深度是否等于1.在批量删除期间,它已经是作业的一部分,因此深度将大于1.如果删除此检查,您的插件应该可以正常工作。