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.
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.
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.
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.
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,
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