Skip to main content

Notifications

Announcements

No record found.

Customer experience | Sales, Customer Insights,...
Suggested answer

Understanding Plugin's Depth and infinite loops

(0) ShareShare
ReportReport
Posted on by 8

I created a custom entity City. 

City has 3 fields - the built-in new_name field, new_value (int), new_description (string).

I created a WF (async) that runs when a new record is being created, or when either description or value change. The WF simply concatenates value and description and updates the name (e.g. new_name = new_value new_description)

Now, I also created a Plugin with the following step (step also contains both pre and post images) :

  • Message: Update
  • Filtering Attributes: new_description, new_name
  • Stage: Post-Operation
  • Mode: Synchronous

The plugin simply updates the value if either description or name have really changed, similar to the WF. (e.g. new_value = new_name new_description)

Questions:

  1. In theory, will this cause an infinite loop? If I change the new_value of the record, the WF runs and updates the new_name which then triggers the plugin to run and update the value, which then triggers the WF to run and so on.
  2. How will this affect context.Depth? will it always be equal to 1? Does the depth increase only when the plugin gets triggered from within itself?

For testing purposes, I have disabled the WF and currently trying to understand why the above described plugin still goes into an infinite loop (triggered multiple times when record is being saved)

This is my code:

 public void Execute(IServiceProvider serviceProvider)
        {
            // Obtain the tracing service
            ITracingService tracingService =
            (ITracingService)serviceProvider.GetService(typeof(ITracingService));

            // Obtain the execution context from the service provider.  
            IPluginExecutionContext context = (IPluginExecutionContext)
                serviceProvider.GetService(typeof(IPluginExecutionContext));

            // The InputParameters collection contains all the data passed in the message request.  
            if (context.InputParameters.Contains("Target") &&
                context.InputParameters["Target"] is Entity)
            {
                // Obtain the target entity from the input parameters.  
                Entity entity = (Entity)context.InputParameters["Target"];
                // Obtain the organization service reference which you will need for  
                // web service calls.  
                IOrganizationServiceFactory serviceFactory =
                    (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
                IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);

                try
                {
                    if (context.Depth > 1) return;

                    if (context.PreEntityImages.Contains("PreImage") && context.PreEntityImages["PreImage"] is Entity
                        && context.PostEntityImages.Contains("PostImage") && context.PostEntityImages["PostImage"] is Entity)
                    {
                        Entity preImage = context.PreEntityImages["PreImage"];
                        string preName = preImage.GetAttributeValue("cre9f_name");
                        string preDesc = preImage.GetAttributeValue("cre9f_s_description");

                        Entity postImage = context.PostEntityImages["PostImage"];
                        string postName = postImage.GetAttributeValue("cre9f_name");
                        string postDesc = postImage.GetAttributeValue("cre9f_s_description");

                        if ((!String.IsNullOrEmpty(preName) || !String.IsNullOrEmpty(preDesc))
                            && (!String.IsNullOrEmpty(postName) || !String.IsNullOrEmpty(postDesc)))
                        {
                            tracingService.Trace("Pre Name: {0}, Post Name: {1}, Pre Desc: {2}, Post Desc: {3}", preName, postName, preDesc, postDesc);

                            Entity update = new Entity(entity.LogicalName);
                            update.Id = entity.Id;
                            tracingService.Trace("Updating entity {0} with GUID {1}", update.LogicalName, update.Id);

                            if (preName != postName && preDesc == postDesc)
                                update["cre9f_s_value"] = postName   " - "   preDesc;
                            else if(preName == postName && preDesc != postDesc)
                                update["cre9f_s_value"] = preName   " - "   postDesc;
                            else if(preName != postName && preDesc != postDesc)
                                update["cre9f_s_value"] = postName   " - "   postDesc;
                            else
                                update["cre9f_s_value"] = preName   " - "   preDesc;

                            tracingService.Trace("Updating field {0}", update["cre9f_s_value"].ToString());

                            service.Update(update); // throws error here
                            tracingService.Trace("Update succeeded");

                        }
                    }
                }

                catch (FaultException ex)
                {
                    throw new InvalidPluginExecutionException("An error occurred.", ex);
                }

                catch (Exception ex)
                {
                    tracingService.Trace("An error occurred: {0}", ex.ToString());
                    throw;
                }
            }
        }

P.S. I'm using the Sales Trial version, if that matters.

  • Ray Profile Picture
    Ray 1,469 on at
    RE: Understanding Plugin's Depth and infinite loops

    I do not think your plugin will go into an infinite loop, it will throw an exception because of the wrong value type, your "cre9f_s_value" field is an Int and you set value with a String.

    Platform use the context.Depth to avoid infinite loop, the default MAX value is 8, if the depth is less than 8, you would not get the infinite loop error.

    Async context is different with sync context, so your WF's context has different depth with your plugin.

  • Suggested answer
    Mahendar Pal Profile Picture
    Mahendar Pal 45,095 on at
    RE: Understanding Plugin's Depth and infinite loops

    Hi,

    If you are doing update in the update plugin it will enter into infinite loop, using depth can be tricky sometime as this plugin maybe in initiated from the another plugin so it won't run which can lead to data inconsistency.

    Not sure why you are using WF and plugin as well, you should use just one.

    "How will this affect context.Depth? will it always be equal to 1? Does the depth increase only when the plugin gets triggered from within itself?"  - It is called by itself or by other plugin for example you are updating these fields from other plugin (sometimes we may have requirement to update one entity from another entity)

    Best option to avoid infinite loop is:

    1. Make sure to configure filtered attributes

    2. Use pre-update to inject your values instead of the post

    Check this

    let me know if you have any other query 

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Congratulations 2024 Spotlight Honorees

Kudos to all of our 2024 community stars! 🎉

Meet the Top 10 leaders for December

Congratulations to our December super stars! 🥳

Start Your Super User Journey

Join the ranks of our community heros! 🦹

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,711 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,458 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans