locked
Plugin not firing on delete - stuck, code inside, pls assist. RRS feed

  • Question

  • I want my plugin to execute on create,update, delete:

    I have my following line:

    public PostOpportunityUpdate(): base(typeof(PostOpportunityUpdate)) {
        base.RegisteredEvents.Add(new Tuple < int, string, string, Action < LocalPluginContext >> (40, "Update", "opportunity", new Action < LocalPluginContext > (ExecutePostOpportunityUpdate)));
        // Note : you can register for more events here if this plugin is not specific to an individual entity and message combination.
        // You may also need to update your RegisterFile.crmregister plug-in registration file to reflect any change.
    }

    I have changed this to different versions such as:

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

    I want the same code to fire for all events but I do not want to write the same code in different events (create,update,delete) so as to pad the code. 

    I have tried:

    if ((context.MessageName == "Create") || (context.MessageName == "Update") || (context.MessageName == "Delete")){        }

    But it is not working. does anyone know the reason?

    Thursday, November 21, 2013 10:09 AM

All replies

  • Try this;

    The point to note is that (is EntityReference) is used for delete.

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

    Thursday, November 21, 2013 11:10 AM
  • i have tried the above as well before but i still have the same problem. For instance i have a 10 opportunities open and then I delete one. The value should now be 9. But I still see 10 opportunities. I am thinking that the plugin is not firing on the Delete event. 
    Thursday, November 21, 2013 11:32 AM
  • Hi,

    You also need to register the events for create and delete. This is how the start of your code should look like:

    base.RegisteredEvents.Add(new Tuple < int, string, string, Action < LocalPluginContext >> (40, "Update", "opportunity", new Action < LocalPluginContext > (ExecutePostOpportunityUpdate)));

    base.RegisteredEvents.Add(new Tuple < int, string, string, Action < LocalPluginContext >> (40, "Create", "opportunity", new Action < LocalPluginContext > (ExecutePostOpportunityUpdate)));

    base.RegisteredEvents.Add(new Tuple < int, string, string, Action < LocalPluginContext >> (40, "Delete", "opportunity", new Action < LocalPluginContext > (ExecutePostOpportunityUpdate)));

    As Hussain said, you will get an entityreference on delete event, and entity on create and update. Also do not forget to add the necessary steps to your solution.

    Regards,

    Adam

    Thursday, November 21, 2013 12:02 PM
  • Borsik, i am still not getting anywhere with the line you provided. Even after registering for create,update,delete. The logic in the code is not firing at all for delete events.
    Thursday, November 21, 2013 12:26 PM
  • Hi Summit,

    When the Delete message is triggered the context.InputParameters["Target"] is of type EntityReference and not Entity.So you have to add a check there. And please make it a pre plugin.

    if (context.MessageName == "Delete")

    {

    if (context.InputParameters.Contains("Target") &&
            context.InputParameters["Target"] is EntityReference)
          {
            EntityReference entityRef = (EntityReference)context.InputParameters["Target"];
            Entity ent=service.Retrieve("<Entity_Name>",entityRef.Id,new  ColumnSet(true));
          }

          }

    If this post answers your question, please click "Mark As Answer" on the post and "Vote as Helpful" Thank you, DT


    Thursday, November 21, 2013 1:54 PM