Nov 1, 2012

Sample Plug-in: Delete

It is important to do something, when a record is being deleted. For that we need to use a plug-in that catches the pre-delete action.

Difference of this approach is we retrieve entity reference instead of entity. Then we can retrieve the other attributes through retrieve method since we got the Id. (While we are in Pre stage we still have time to grab them.)

Check the code as below;

using System;
using System.Collections.Generic;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
using System.ServiceModel;

namespace TestCompany.CRM.Plugin
{
 public class officePreDelete : IPlugin
 {
   public void Execute(IServiceProvider serviceProvider)
   {
     IPluginExecutionContext context;
     IOrganizationServiceFactory factory;
     IOrganizationService service;

     try
     {
       context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
       if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is EntityReference)
       {
         EntityReference EntityRef = (EntityReference)context.InputParameters["Target"];
         if (EntityRef.LogicalName != "new_office")
             return;
                     
         factory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
         service = factory.CreateOrganizationService(context.UserId);
                   
         // Do the logic as required
         // use EntityRef.Id retrive other attributes

        }
      }
      catch (FaultException<OrganizationServiceFault> e)
      {
        throw e;
      }
      finally
      {
        service = null;
        factory = null;
        context = null;
      }
   }
 }
}

Now check the plug-in registration setting;

 
Important Note
Pl note, though we are in pre-Delete stage, record has already lost the relationships with other records. This means you are not able to query other records using the values in deleting record. If you need to do, what needs to do is register the plug-in step in Pre-validation stage than Pre-operation.
 
Related posts;
Sample Plug-in: Compare Pre and Post images on Update
Sample Plug-in code: Create and Update
Retrieve attributes of entity object


6 comments:

  1. I need plugin code for "when parent custom entity record is deleted, automatically related child entity record could be deleted"

    Thanks in advance(Mail : Chandrasekharreddy350@gmail.com)

    ReplyDelete
  2. You can simply configure the relationship to Parental. (Currently it could be referential). Then your requirement will be satisfied.

    ReplyDelete
  3. For example 2 custom entities without relationship, we have to delete the second entity by validate values with other entity?

    ReplyDelete
  4. Can you plz tell me for when will we go with pre-validation, pre-operation & post operation with one scenario in single plugin of all operations?

    ReplyDelete
  5. Chandu, When you say "without relationship", how you identify which record to delete? If you have a criteria to identify, you can call delete method without any issue.

    ReplyDelete
  6. Chandu, Pl read this post for second question. http://sumedha8.blogspot.com.au/2014/09/plug-in-concerns-synchronous-or.html

    ReplyDelete