0
votes

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
Can you please share the code. So that I can see if there is any issue.Scorpion
Just a guess - try to register plugin in asynchronous mode or move the logic to workflow.Scorpion
thank you for your reply. i tried with the Asynchronous mode as well. but it did not work.user3562857

1 Answers

2
votes

In your code you are checking if the context depth is equal to 1. During a bulk delete it is already part of a job so the depth will be greater than 1. If you remove this check your plugin should work.