Another MS CRM question from me, I\'m afraid. I\'ve got the following code being executed on the update of a contact record but it gives me an error saying the job was cance
it's happening because your plugin is executed when a contact is updated and the last line of your code update the contact again, this cause to call again the plugin ...
Then you have your infinite loop
You can prevent the loop using the IExecutionContext.Depth
property
http://msdn.microsoft.com/en-us/library/microsoft.xrm.sdk.iexecutioncontext.depth.aspx
However if you explain your requirement I think it's possible to find a solution.
**I went through a lot of trial and error. I don't know why plugin context does not work but this works but the parentcontext works. This (workaround?) works :) **
if (this.Context.ParentContext != null && this.Context.ParentContext.ParentContext != null)
{
var assemblyName = Assembly.GetExecutingAssembly().GetName().Name;
if (!this.Context.ParentContext.ParentContext.SharedVariables.Contains(assemblyName))
{
this.Context.ParentContext.ParentContext.SharedVariables.Add(assemblyName, true.ToString() );
}
else
{
// isRecursive = true;
return;
}
}
At first if IExecutionContext.Depth <= 1
seems like a great idea, but it can bite you if you have a different plugin that updates the contact. You should be using the SharedVariables of the plugin context.
Something like this should work:
Add this declaration to the plugin class as a class level field:
public static readonly Guid HasRunKey = new Guid("{6339dc20-01ce-4f2f-b4a1-0a1285b65bff}");
And add this as the first step of your plugin:
if(context.SharedVariables.ContainsKey[HasRunKey]){
return;
}else{
context.SharedVariables.Add(HasRunKey);
// Proceed with plugin execution
}
Your plugin is updating the "jobtitle" field, I'm not sure if this plugin is being triggered by all contact updates, or you have set some FilteringAttributes to it in the Registerfile.crmregister Plugin's definition. By excluding the "jobtitle" field from the attributes that trigger this plugin you can solve your issue.