Getting Deleted Entity Data Within Your CRM 2011 Plugin

30 Aug
2012

Recently we needed to access an entity records data within a Delete plugin step in order to delete other related data. The steps involved are slightly different from a create or update but also the step will need to be registered as “pre-operation”.

So normally we would access the entity by using the following code.


// The InputParameters collection contains all the data passed in the message request.
if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
{
   // Obtain the target entity from the input parmameters.
   Entity entity = (Entity)context.InputParameters["Target"];

   // If not, this plug-in was not registered correctly.
   if (entity.LogicalName == "entity_name")
   {
      .......................

So instead of being able to access the records data directly, we need to access it by using the EntityReference that is being passed. Once we have the entityreference we can then query the service to retrieve the relevant data as shown below.


// The InputParameters collection contains all the data passed in the message request.
if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is EntityReference)
{
   // Obtain the target entity from the input parmameters.
   EntityReference EntityRef = (EntityReference)context.InputParameters["Target"];
   
   // If not, this plug-in was not registered correctly.
   if (EntityRef.LogicalName == "entity_name")
   {
      //Lets get the image of the record to be deleted
      Entity entityImage = service.Retrieve(EntityRef.LogicalName, EntityRef.Id, new ColumnSet(new string[] { "entity_column_1", "entity_column_2" }));

      //Lets check what the entity name is
      string column_1_value = entityImage.Attributes["entity_column_1"].ToString();
      string column_2_value = entityImage.Attributes["entity_column_2"].ToString();
      .......................

Hope this helps anyone out there.

Leave a Reply