Quantcast
Channel: Microsoft Dynamics CRM » CRM 2013
Viewing all articles
Browse latest Browse all 5

CRM 2015 Plugins – Avoiding Infinite Loops

$
0
0

This has been a pain for quite some time and have been using (localContext.PluginExecutionContext.Depth > 1) to avoid the infinite plugin loop. BUT this also creates another problem when the plugin suppose to execute either during data import or when using a workflow to create/update.

Problem SOLVED by using localContext.SharedVariables in the plugin. First you need a function to check if the SharedVariables exist before executing your plugin logic, if it does not exist then add it. The trick is to check the SharedVariables in the parent context of the context. See below code:

protected void ExecutePostCaseUpdate(LocalPluginContext localContext)
{
if (localContext == null)
{
throw new ArgumentNullException(“localContext”);
}

if (!CheckRecursionIndicator(localContext.PluginExecutionContext, “ExecutePostCaseUpdateIndicator”)) return;
var updateCase = ((Entity)localContext.PluginExecutionContext.InputParameters[“Target”]).ToEntity();

// TODO: Implement your custom Plug-in business logic.
updateCase.Title = “Plugin Shared Viarable Test”;
localContext.OrganizationService.Update(updateCase);
}

///
/// Check plugin loop recursion
///

/////////
private bool CheckRecursionIndicator(IPluginExecutionContext context, string recursionIndicator)
{
if (!context.SharedVariables.Contains(recursionIndicator))
{
while (context.ParentContext != null)
{
if (context.ParentContext.SharedVariables.Contains(recursionIndicator))
{
return false;
}
context = context.ParentContext;
}
}

context.SharedVariables.Add(recursionIndicator, (Object)true);
return true;
}



Viewing all articles
Browse latest Browse all 5

Latest Images

Trending Articles





Latest Images