SetState and SetStateDynamicEntity plugin fires twice
-
Wednesday, May 09, 2012 12:40 PM
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?
All Replies
-
Wednesday, May 09, 2012 1:39 PMIt 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 09, 2012 1:55 PMI'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 09, 2012 4:11 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 09, 2012 9:07 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
- Proposed As Answer by Dimaz Pramudya (www.xrmbits.com) Wednesday, May 09, 2012 9:07 PM
- Marked As Answer by ouufff Sunday, June 17, 2012 2:50 PM