Sunday, June 24, 2018

What is plugin dept in MS CRM Or stopping infinite loop in Plugin



Depth is often used to stop infinite loops where a plugin updates a value which triggers the plugin to run again.
A common cause of this is bad programming practice where a plugin retrieves the target entity and at the end of the plugin updates the entity.

e.g. 

1. A post plugin triggered on update of account name.
2. Plugin retrieves target entity
3. Plugin updates name field on target entity field to name + unique number
4. Plugin does CrmService.Update (target entity);

Plugin has updated field value which triggers plugin again The way around this is to put code in a pre plugin. 

It’s possible to check the depth of the plugin-

IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));

            if(context.Depth > 1)

                return;

Every time a running plug-in or Workflow issues a message request to the Web services that triggers another plug-in or Workflow to execute, the Depth property of the execution context is increased. If the depth property increments to its maximum value within the configured time limit, the platform considers this behavior an infinite loop and further plug-in or Workflow execution is aborted.
The maximum depth (8) and time limit (one hour) are configurable by the Microsoft Dynamics 365 administrator using the PowerShell command Set-CrmSetting.
The setting is WorkflowSettings.MaxDepth. For more information, see, “Administer the deployment using Windows PowerShell” in the Deploying and administering Microsoft Dynamics CRM.


No comments:

Post a Comment

QueryExpression vs. FetchXML in MS CRM with C#

Microsoft Dynamics CRM (Customer Relationship Management) is a powerful platform that helps organizations streamline their business processe...