Friday, March 2, 2018

MS CRM Plugin Execution Context - Depth [Back to Basics]


Plugin Execution context holds properties that contain information about the run-time environment.

Depth - Integer value that holds the depth of execution within a call stack/transaction.

Depth Property is used by the platform to avoid infinite loop.

By default, the depth value is 1 when initiated from platform.

When a plugin/workflow is initiated from another plugin/workflow depth increments by 1.

Infinite Loop:

Validating depth to prevent infinite loop will fail in the below scenario:

1. When a plugin/workflow is initiated from another plugin/workflow. Validating depth here will prevent the logic from being run since it will always be more than 1.

2. When the logic is initiated from bulk import wizard. Default value returned from the context initiated from bulk import begins with 2.

Yet another property along with depth that gets populated when called from another Plugin/workflow is ParentContext.

ParentContext value is null when initiated from platform or holds context object of the triggering plugin/workflow.

On a pursuit to prevent plugin recursive call, we are risking its execution when called from another process other than platform.

To avoid recursive calls, 

1. Monitor to register the plugin with filter by attributes

2. Do not update the target entity in an update plugin (most common case of infinite loop in a plugin) rather create an instance of the target entity and include attributes that requires update and not all.


Platform comes up with a restriction, set in WorkflowSettings.MaxDepth

Max Depth : 8
Max time : 1 hour

Custom logic that reached max depth in the max time limit, is aborted by the platform as a infinite loop behaviour. 

Possible to override this setting from poweshell command, however is not recommended. 

MS CRM Plugin Execution Context - Depth [Back to Basics]

Plugin Execution context holds properties that contain information about the run-time environment. Depth - Integer value that holds t...