Wednesday, 5 April 2017

MS CRM - IPluginExecutionContext.Depth a detailed look - #PluginTip1


Today, we are going to have a detailed look on the depth property available in plugin or workflow context. By the end of this post, you will have enough knowledge to decide when to use depth and how to use it wisely.

Intro:
Depth property is used to find the position of the plugin in current call stack. To be simple, with this property we can find whether the plugin is triggered by another process(plugin/workflow) or directly by platform/user.

Default depth value is 1. 

If plugin depth is greater than 1, then it is triggered by another process. Depth of the plugin is incremented by 1, if called by another process.

Myth
There is a myth around depth, that it can be used to break infinite loops. Yes they can, but these checks may become dangerous and may skip your plugin logic at times. For example, If a plugin from account triggers a plugin in contact, the depth in contact plugin is 2 and that is not an infinite loop.

Example 1:
Lets take an example for infinite loop,
On an account update(post-operation) we have a plugin and this plugin updates the account again, This update triggers the same plugin again.

Here for the first time depth would be 1 and from then on every time it will be incremented by 1. So to avoid loop, we should to check the depth but personally I don't prefer using depth here.

Example 2:
Lets take an example for parent child,
On an account update post-operation we have a plugin and this plugin updates a contact record which in-turn triggers another plugin. If we may need to restrict the plugin execution on contact, only when there is a direct operation on contact and not from account. We can use depth to achieve this.

Here for the first plugin depth would be 1 and for the child/triggered plugin the depth would be 2. You can use depth validation to exit the plugin,

Conclusion:
Be wise when using depth validation!

Feel free to comment your views, hope this post is informative.

Also check out my blog on IntitatingUserId and UserId

6 comments:

  1. Really informative, triggered my thoughts in order to read more in depth about this depth property.. Thank you :)

    ReplyDelete
  2. Is any other way we can skip infinite loop in palce of depth

    ReplyDelete
  3. You can use filtering attribute or by using pre-operation stage. It depends on scenario.

    ReplyDelete
  4. Thanks for providing recent updates regarding the concern, I look forward to read more.
    Microsoft Dynamics 365

    ReplyDelete

Contact Form

Name

Email *

Message *