locked
SetState and SetStateDynamicEntity plugin fires twice RRS feed

  • Question

  • I have a plugin that is registered on both these messages for a serviceappointment on prevalidation so that a code executes whenever the serviceappointment gets closed.

    Sometimes the plugin gets called twice in the same minute for the same record.

    Is it possible because the plugin is registered on both these messages that this double call happens?

    Wednesday, May 9, 2012 12:40 PM

Answers

  • While the SetState and SetStateDynamicEntity message should not get fired at the same time, there could be a scenario of when that could happen.

    One would be if you Deactivate a parent record, all the child record will get triggered. When this happen, the SetState / SetStateDynamicEntity will get triggered on both parent and child records.

    In the event of this, the best way is to check for the context.PrimaryEntityName to ensure it matches the entity name that you want your logic to run on.

    Hope this helps.


    Dimaz Pramudya - CRM Developer - CSG (Melbourne) www.xrmbits.com http://twitter.com/xrmbits

    Wednesday, May 9, 2012 9:07 PM

All replies

  • It has been recommended to register the plugin for both messages.  I don't believe it should fire twice.  Are you sure there isn't custom code that is triggering another SetState?

    Blake Scarlavai - http://mscrmdev.blogspot.com/ - Sonoma Partners - http://www.sonomapartners.com/ - Follow @bscarlav

    Wednesday, May 9, 2012 1:39 PM
  • I'm pretty sure that there isn't other code triggering the SetState. I also read a post that recommends registering on both messages, but wanted to ask here just in case anyone knew of a situation when this happens.
    Wednesday, May 9, 2012 1:55 PM
  • It sounds simliar to an issue you can get on an Update plugin message.

    You can add the below at the start of your code and this may get round this, as this works on the Update message.

    if (context.depth <= 1)

    {

    //Execute main plugin logic

    }

     

    Wednesday, May 9, 2012 4:11 PM
  • While the SetState and SetStateDynamicEntity message should not get fired at the same time, there could be a scenario of when that could happen.

    One would be if you Deactivate a parent record, all the child record will get triggered. When this happen, the SetState / SetStateDynamicEntity will get triggered on both parent and child records.

    In the event of this, the best way is to check for the context.PrimaryEntityName to ensure it matches the entity name that you want your logic to run on.

    Hope this helps.


    Dimaz Pramudya - CRM Developer - CSG (Melbourne) www.xrmbits.com http://twitter.com/xrmbits

    Wednesday, May 9, 2012 9:07 PM