Search code examples
dynamics-crm-2011dynamics-crm-2013dynamics-crm-online

How To Allow CRM Plugin To Handle Multiple Event Messages


I need to create a single plugin to fire on create, update, and delete events of opportunity entities.

Can I do these in a single plugin? If so then how can I do them?


Solution

  • yes you can use same plugin to execute it on create,update and delete

    write your plugin as follows..

        public void Execute(IServiceProvider serviceProvider)
        {
              IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
               IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
               IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
    
               if (context.MessageName == "Create") 
               {
                 //write the logic what you want this plugin to do on Create
               }
               if (context.MessageName == "Update") 
               {
                //write the logic what you want this plugin to do on Update
               }
               if (context.MessageName == "Delete") 
               {
                //write the logic what you want this plugin to do on Delete
               }  
    }
    

    Register your plugin on Create,Update,Delete for Oppertunity Entity using Plugin registration tool

    it should work