dynamics crm 2011 - Microsoft CRM Plugin Infinite Loop -
another ms crm question me, i'm afraid. i've got following code being executed on update of contact record gives me error saying job cancelled because includes infinite loop. can tell me why happening, please?
// <copyright file="postcontactupdate.cs" company=""> // copyright (c) 2013 rights reserved // </copyright> // <author></author> // <date>8/7/2013 2:04:26 pm</date> // <summary>implements postcontactupdate plugin.</summary> // <auto-generated> // code generated tool. // runtime version:4.0.30319.1 // </auto-generated> namespace plugins3test { using system; using system.servicemodel; using microsoft.xrm.sdk; using microsoft.xrm.sdk.query; /// <summary> /// postcontactupdate plugin. /// fires when following attributes updated: /// attributes /// </summary> public class postcontactupdate: plugin { /// <summary> /// initializes new instance of <see cref="postcontactupdate"/> class. /// </summary> public postcontactupdate() : base(typeof(postcontactupdate)) { base.registeredevents.add(new tuple<int, string, string, action<localplugincontext>>(40, "update", "contact", new action<localplugincontext>(executepostcontactupdate))); // note : can register more events here if plugin not specific individual entity , message combination. // may need update registerfile.crmregister plug-in registration file reflect change. } /// <summary> /// executes plug-in. /// </summary> /// <param name="localcontext">the <see cref="localplugincontext"/> contains /// <see cref="ipluginexecutioncontext"/>, /// <see cref="iorganizationservice"/> /// , <see cref="itracingservice"/> /// </param> /// <remarks> /// improved performance, microsoft dynamics crm caches plug-in instances. /// plug-in's execute method should written stateless constructor /// not called every invocation of plug-in. also, multiple system threads /// execute plug-in @ same time. per invocation state information /// stored in context. means should not use global variables in plug-ins. /// </remarks> protected void executepostcontactupdate(localplugincontext localcontext) { if (localcontext == null) { throw new argumentnullexception("localcontext"); } // todo: implement custom plug-in business logic. // obtain execution context service provider. ipluginexecutioncontext context = localcontext.pluginexecutioncontext; iorganizationservice service = localcontext.organizationservice; iserviceprovider serviceprovider = localcontext.serviceprovider; itracingservice tracingservice = localcontext.tracingservice; // obtain target entity input parmameters. //entity contextentity = (entity)context.inputparameters["target"]; entity targetentity = null; targetentity = (entity)context.inputparameters["target"]; guid cid = targetentity.id; columnset cols = new columnset("jobtitle"); entity contact = service.retrieve("contact", cid, cols); contact.attributes["jobtitle"] = "sometitle"; service.update(contact); } } }
it's happening because plugin executed when contact updated , last line of code update contact again, cause call again plugin ...
then have infinite loop
you can prevent loop using iexecutioncontext.depth
property
http://msdn.microsoft.com/en-us/library/microsoft.xrm.sdk.iexecutioncontext.depth.aspx
however if explain requirement think it's possible find solution.
Comments
Post a Comment