Tuesday, 15 March 2011

"Delete event is not fired in CRM 2011" / "Delete event is not working in CRM 2011"

Have you ever faced the above issue? If so lets figure out why its happening? In simple words the Villian is context.InputParameters["Target"

Delete event -> Type of context.InputParameters["Target"]  is EntityReference
Create event / Update event ->Type of context.InputParameters["Target"] is Entity

In a nutshell,
  
context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
if (context.InputParameters.Contains("Target") &&
 context.InputParameters["Target"] is EntityReference)
{
if (context.MessageName == "Delete")
{
 // Code to be executed during Delete event
}
}

Whereas Create / Update events work as follows

context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));


if (context.InputParameters.Contains("Target") &&
 context.InputParameters["Target"] is Entity)
{
  
 if (context.MessageName == "Create")
 {
//Code to be executed during Create event of an entity
}

   if (context.MessageName == "Update”)
{
//Code to be executed during Update event of an entity
}

}


Please note the key difference Entity and EntityReference

No comments:

Post a Comment