Dynamics CRM 2011 PostUpdate gets caught an infinite loop

I am developing a plug-in. When I update the entity in PostUpdate plug-in, PreUpdate plug-in is triggered PostUpdate plug-in and it gets caught in an infinite loop.

How can I break infinite loop ?

Upvotes: 0

Views: 138

Answers (2)

Malachy
Malachy

Reputation: 392

I would just like to add that one of the main reasons that a CRM plugin gets caught in an infinite loop is that the plugin is using the CRM Service to update the record that triggered the plugin in the first place.

Rather than checking for recursion using the dept property it is often a better approach to simply perform the update to the CRM record using Target Image.

The following post addresses a similar question: crm 2011 Updating the record that fired the Plugin in post-sync and post-async stage

There are cases where this sort of recursion is unavoidable; but it is often worth understanding why is the recursion occurring and can it be avoided.

Upvotes: 0

Rahul Purush
Rahul Purush

Reputation: 125

Use the Depth property to break out of the infinite loop.

Within your plugin, check if the PluginExecutionContext.Depth is greater 1 and if so, return.

Relevant code snippet.

if (localContext.PluginExecutionContext.Depth > 1)
{
    return;
}

Upvotes: 1

Related Questions