简体   繁体   中英

Bulk delete does not fire the plugin in CRM dynamics

I want to trigger a plugin from bulk deletion in Microsoft CRM dynamics 2013. I created a custom entity and my plugin pointed to the delete operation of this custom entity. I configured the plugin registration as follows:

   Message :    "delete"
   Primary entity :     "my custom entity",
   Run in User's Context :    "Calling User",
   Event pipeline :    "Pre operation",
   Execution mode :    "synchronous"

The bulk delete process isn't firing the plugin. it isn't throwing an error.
However the plugin can trigger by manual delete operation.but I want to fire the plugin from any system event as a schedule (bulk delete)
Please help me to solve this issue.

This is my code:

 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.如果删除此检查,您的插件应该可以正常工作。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM