locked
[CRM 4.0] Plugin starts twice when I cancel an appointment RRS feed

  • Question

  • Hi

    I'm trying to create a plugin that will fire when something changes on an Appointment. After sorting out a problem with it not fiering on Cancel (http://social.msdn.microsoft.com/Forums/en-US/6b5bbae4-d6a3-492e-9440-ca656a0abcee/crm-4-how-to-run-a-plugin-on-cancel-of-an-appointment?forum=crmdevelopment) I've run into another one: when I cancel an Appointment it runs twice - the first time it shows as Open, the second as Closed.

    The plugin is configured to run on the SetStateDynamicEntity message.

    Below is my code:

    DynamicEntity Entity = null;
    
    using (CrmService _service = CreateCrmService())
    {
       Moniker _moniker = (Moniker)context.InputParameters.Properties["EntityMoniker"];
    
       TargetRetrieveDynamic target = new TargetRetrieveDynamic();
       target.EntityName = _moniker.Name;
       target.EntityId = _moniker.Id;
    
       RetrieveRequest getApp = new RetrieveRequest();
       getApp.ReturnDynamicEntities = true;
       getApp.Target = target;
       getApp.ColumnSet = new ColumnSet(new string[5]{"activityid", "statecode", "ds_typpoi", "scheduledstart", "scheduledend"});
       RetrieveResponse retrieved = (RetrieveResponse)_service.Execute(getApp);
       Entity = (DynamicEntity)retrieved.BusinessEntity;
    }
    string statecode = string.Empty;
    int ds_typpoi = -1;
    string scheduledstart = string.Empty;
    string scheduledend = string.Empty;
    
    if (Entity.Properties.Contains("statecode"))
       statecode = Convert.ToString(Entity.Properties["statecode"]);
    

    When I change the appointments status to 'Canceled' the plugin runs twice. On the first run the value of the statecode variable is 'Open', on the second it's 'Canceled'. The problem is, that when I reopen the Appointment the statecode is also 'Open' and I need to do separate things on the two events.

    So my question is: is there a way to filter out the fake "Open" run of the plugin, or at least to tell where it came from so I can ignore it in the code?

    Friday, November 22, 2013 3:36 PM

Answers

  • I found the reason: in the declaration of the plugin messages, under Book and Reschedule I've had "Both" set as ImageType. After changing to PostImage it started to behave normally and only starts once after a cancel.

    No idea why it matters, since these are separate messages, but I won't complain :)

    • Marked as answer by Grzegorz Zych Tuesday, November 26, 2013 12:45 PM
    Tuesday, November 26, 2013 12:45 PM

All replies

  • Hi,

    Is your plugin running under only one message SetStateDynamicEntity ?


    Hope this helps. If you get answer of your question, please mark the response as an answer and vote as helpful !
    Vikram !

    Friday, November 22, 2013 4:16 PM
  • Hi,

    Can you please explain me a bit clear. When you want to fire your plugin?. On which step and message you registered your plugin .

    Share the total code you have been using .


    Warm Regards, Suresh Kumar D

    Monday, November 25, 2013 4:10 AM
  • Hi,

    The plugin runs under the following messages: Book, Reschedule, SetStateDynamicEntity. There are no other plugins that start on appointment.

    I want to fire the plugin every time something is changed on the appointment and take action depending on what happened.

    Below the entire Execute method - the plugin is unfinished, so it does not really do anything yet.

    public void Execute(IPluginExecutionContext context)
            {
                DynamicEntity Entity = null;
    
                string _m = context.MessageName;
    
                switch (context.MessageName)
                {
                    case "Book":
                    case "Reschedule":
                        if (context.PostEntityImages.Contains("ObjectImage"))
                            Entity = (DynamicEntity)context.PostEntityImages["ObjectImage"];
                        break;
                    case "SetState":
                    case "SetStateDynamicEntity":
                        using (CrmService _service = BaseFuntions.CreateCrmService())
                        {
                            Moniker _moniker = (Moniker)context.InputParameters.Properties["EntityMoniker"];
    
                            TargetRetrieveDynamic target = new TargetRetrieveDynamic();
                            target.EntityName = _moniker.Name;
                            target.EntityId = _moniker.Id;
    
                            RetrieveRequest getApp = new RetrieveRequest();
                            getApp.ReturnDynamicEntities = true;
                            getApp.Target = target;
                            getApp.ColumnSet = new ColumnSet(new string[5]{"activityid", "statecode", "ds_typpoi", "scheduledstart", "scheduledend"});
                            RetrieveResponse retrieved = (RetrieveResponse)_service.Execute(getApp);
                            Entity = (DynamicEntity)retrieved.BusinessEntity;
                        }
                        break;
                    default:
                        return;
                }
    
                if (Entity == null)
                    return;
    
                Guid _appointmentId = Guid.Empty;
    
                if (Entity.Properties.Contains("activityid"))
                    _appointmentId = ((Key)Entity.Properties["activityid"]).Value;
                else
                    return;
    }

    When I cancel the appointment the value of '_m' is "SetStateDynamicEntity" in both instances.
    Tuesday, November 26, 2013 11:36 AM
  • I found the reason: in the declaration of the plugin messages, under Book and Reschedule I've had "Both" set as ImageType. After changing to PostImage it started to behave normally and only starts once after a cancel.

    No idea why it matters, since these are separate messages, but I won't complain :)

    • Marked as answer by Grzegorz Zych Tuesday, November 26, 2013 12:45 PM
    Tuesday, November 26, 2013 12:45 PM