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);
}
}
}