locked
Plugin error on update entity field RRS feed

  • Question

  • Trying to update incident entity Description field on Case update. And got:

    On Case create the same construction works fine. What's wrong?

    namespace MyPluginPackage.Plugin
    {
        using System;
        using System.ServiceModel;
        using Microsoft.Xrm.Sdk;
    
        public class PostCaseUpdate: Plugin
        {
    
            public PostCaseUpdate()
                : base(typeof(PostCaseUpdate))
            {
                base.RegisteredEvents.Add(new Tuple<int, string, string, Action<LocalPluginContext>>(40, "Update", "incident", new Action<LocalPluginContext>(ExecutePostCaseUpdate)));
            }
    
            protected void ExecutePostCaseUpdate(LocalPluginContext localContext)
            {
                if (localContext == null)
                {
                    throw new ArgumentNullException("localContext");
                }
                IPluginExecutionContext context = localContext.PluginExecutionContext;
                IOrganizationService service = localContext.OrganizationService;
                if (context.InputParameters.Contains("Target") &&
                context.InputParameters["Target"] is Entity)
                {
                    Entity entity = (Entity)context.InputParameters["Target"];
                    try
                    {
                        entity["title"] = "Any Text";
                        service.Update(entity);
                    }
                    catch (FaultException ex)
                    {
                        throw new InvalidPluginExecutionException("An error occurred in the plug-in.", ex);
                    }
                }
            }
        }
    }
    

    Thanks!


    Если сообщение оказалось полезным, пожалуйста, проголосуйте за него или пометьте в качестве ответа.

    Wednesday, May 28, 2014 11:35 AM

Answers

  • The Problem is that you are forcing it into an endless loop.Use this code.

    namespace MyPluginPackage.Plugin
    {
        using System;
        using System.ServiceModel;
        using Microsoft.Xrm.Sdk;
    
        public class PostCaseUpdate: Plugin
        {
    
            public PostCaseUpdate()
                : base(typeof(PostCaseUpdate))
            {
                base.RegisteredEvents.Add(new Tuple<int, string, string, Action<LocalPluginContext>>(40, "Update", "incident", new Action<LocalPluginContext>(ExecutePostCaseUpdate)));
            }
    
            protected void ExecutePostCaseUpdate(LocalPluginContext localContext)
            {
                if (localContext == null)
                {
                    throw new ArgumentNullException("localContext");
                }
                IPluginExecutionContext context = localContext.PluginExecutionContext;
                IOrganizationService service = localContext.OrganizationService;
                if (context.InputParameters.Contains("Target") &&
                context.InputParameters["Target"] is Entity  && context.Depth < 2)
                {
                    Entity entity = (Entity)context.InputParameters["Target"];
                    try
                    {
                        entity["title"] = "Any Text";
                        service.Update(entity);
                    }
                    catch (FaultException ex)
                    {
                        throw new InvalidPluginExecutionException("An error occurred in the plug-in.", ex);
                    }
                }
            }
        }
    }
    context.Depth < 2
    

    • Proposed as answer by F.Kuyler Wednesday, May 28, 2014 11:48 AM
    • Marked as answer by Dismantled Wednesday, May 28, 2014 12:08 PM
    Wednesday, May 28, 2014 11:47 AM