locked
Plugin is not triggering on User Enable/Disable RRS feed

  • Question

  • Hi All,

    I am using Dynamics 365 Online Trail. I have an requirement to trigger a plugin when the user is disabled/enabled in CRM. The plugin will create the record in contact entity when the user is Disabled/Enabled. so created the plugin with below code and registered the plugin in User entity SetState and SetStateDynamicEntity messages but the plugin is not trigered.

    Plugin Registration Steps: Message: SetState & SetStateDynamicsEntity ,Entity: systemuser,Event: Pre-Operation

    Code:

     public void Execute(IServiceProvider serviceProvider)
            {
                IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
                IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
                ITracingService tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
                IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
                try {
                    if (context.InputParameters.Contains("EntityMoniker") && context.InputParameters["EntityMoniker"] is EntityReference &&
    ((EntityReference)context.InputParameters["EntityMoniker"]).LogicalName == "systemuser")
                    {
                        EntityReference user = (EntityReference)context.InputParameters["EntityMoniker"];

                        if (context.InputParameters.Contains("State"))
                        {
                            //Status of the user records 1- Disabled ,0 - Enabled
                            OptionSetValue state = (OptionSetValue)context.InputParameters["State"];

                            OptionSetValue status = (OptionSetValue)context.InputParameters["Status"];

                            CreateContact(service, "Plugin", "Triggered");
                        }
                        else
                        {
                            CreateContact(service, "Plugin", "Not Triggered");
                        }
                    }
                    else { CreateContact(service, "InputParm", "Empty"); }         

                }
                catch (Exception ex) {

                    CreateContact(service, "PluginError", ex.Message);              
                }
            }


            public void CreateContact(IOrganizationService service,string FName,string LName) {

                Entity contact = new Entity("contact");
                contact["firstname"] = FName;
                contact["lastname"] = LName;
                service.Create(contact);

            }

    User records has been Enabled/Disabled in CRM(through O365) but the plugin is not triggered.

    Also tried with update message plugin with below code for the same requirement, but that also not triggering the plugin

    Update Message Registration Steps: Message: Update ,Entity:systemuser ,Event:Pre-Operation ,Filtering Attribute: All Fields , Image: Pre Image

    Note: Also tried with Post Operation and Post Images

    Code:                        

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

                IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));

                ITracingService tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));

                IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
                try
                {
                    Entity PreImage = (Entity)context.PreEntityImages["PreUpdateImage"];
                    //  Entity PostImage = (Entity)context.PostEntityImages["PostUpdateImage"];

                    if (PreImage.Contains("windowsliveid") || PreImage.Contains("isdisabled"))
                    {
                        CreateContact(service, "Plugin", "Triggered");
                    }
                    else
                    {
                        CreateContact(service, "Status", "Empty");
                    }
                }
                catch (Exception ex)
                {
                    CreateContact(service, "Error", ex.Message);
                }
            }

            public void CreateContact(IOrganizationService service, string FName, string LName)
            {
                Entity contact = new Entity("contact");
                contact["firstname"] = FName;
                contact["lastname"] = LName;
                service.Create(contact);
            }

    Update message plugin is not triggering  on user disable/enable.

    Note: the same update message plugin is working as expected,when i update some other fields like Business phone ...etc in user record.

    It looks like Dynamics 365 Product Bug.

    Is there anything changed in Dynamics 365.Kindly let us know your thoughts.

    Thanks in advance.



    • Edited by Gugan A Tuesday, November 29, 2016 7:57 AM
    Tuesday, November 29, 2016 7:50 AM

All replies

  • Trigger you plugin on change of isdisabled field

    Regards Faisal

    Wednesday, November 30, 2016 11:04 AM