locked
Overwrite SetStateRequest when incident gets reactivated RRS feed

  • Question

  • Currently when a user re-activates a case, the statuscode is set to some default option. Without changing any defaults on the entity, I want to set the statuscode to another option based upon some criteria.

    So I've set up a plugin to activate at the PreOperation pipeline and on the SetStateDynamicEntity message. Everything works fine except it looks as though the Request gets sent twice because when I look at the Audit of the case after reactivation, I see two instances of the edit, which is not what I want.

    Is there a better way to do this? And does anyone know why the request is being sent twice?

    Here is the class of the plugin implementation:

    // <copyright file="PreCaseSetStateDynamicEntity.cs" company="">
    // Copyright (c) 2013 All Rights Reserved
    // </copyright>
    // <author></author>
    // <date>12/2/2013 2:43:20 PM</date>
    // <summary>Implements the PreCaseSetStateDynamicEntity Plugin.</summary>
    // <auto-generated>
    //     This code was generated by a tool.
    //     Runtime Version:4.0.30319.1
    // </auto-generated>
    namespace CRM2011Plugins
    {
        using System;
        using System.ServiceModel;
        using Microsoft.Xrm.Sdk;
        using Data.Constants;
        using Microsoft.Crm.Sdk.Messages;
        using Radiant;

        /// <summary>
        /// PreCaseSetStateDynamicEntity Plugin.
        /// </summary>    
        public class PreCaseSetStateDynamicEntity: Plugin
        {
            /// <summary>
            /// Alias of the image registered for the snapshot of the 
            /// primary entity's attributes before the core platform operation executes.
            /// The image contains the following attributes:
            /// ticketnumber,caseorigincode,statecode,statuscode
            /// </summary>
            private readonly string preImageAlias = "preCaseImage";

            /// <summary>
            /// Initializes a new instance of the <see cref="PreCaseSetStateDynamicEntity"/> class.
            /// </summary>
            public PreCaseSetStateDynamicEntity()
                : base(typeof(PreCaseSetStateDynamicEntity))
            {
                base.RegisteredEvents.Add(new Tuple<int, string, string, Action<LocalPluginContext>>((int)Enums.PluginEnums.PipeLineStages.PreOperation, Enums.PluginEnums.Messages.SetStateDynamicEntity.ToString(), Enums.PluginEnums.Entities.Incident.ToString().ToLower(), new Action<LocalPluginContext>(ExecutePreCaseSetStateDynamicEntity)));

                // Note : you can register for more events here if this plugin is not specific to an individual entity and message combination.
                // You may also need to update your RegisterFile.crmregister plug-in registration file to reflect any change.
            }

            /// <summary>
            /// Executes the plug-in.
            /// </summary>
            /// <param name="localContext">The <see cref="LocalPluginContext"/> which contains the
            /// <see cref="IPluginExecutionContext"/>,
            /// <see cref="IOrganizationService"/>
            /// and <see cref="ITracingService"/>
            /// </param>
            /// <remarks>
            /// For improved performance, Microsoft Dynamics CRM caches plug-in instances.
            /// The plug-in's Execute method should be written to be stateless as the constructor
            /// is not called for every invocation of the plug-in. Also, multiple system threads
            /// could execute the plug-in at the same time. All per invocation state information
            /// is stored in the context. This means that you should not use global variables in plug-ins.
            /// </remarks>
            protected void ExecutePreCaseSetStateDynamicEntity(LocalPluginContext localContext)
            {
                if (localContext == null)
                {
                    throw new ArgumentNullException("localContext");
                }

                IPluginExecutionContext context = localContext.PluginExecutionContext;

                Entity preImageEntity = (context.PreEntityImages != null && context.PreEntityImages.Contains(this.preImageAlias)) ? context.PreEntityImages[this.preImageAlias] : null;

                var caseOriginCode = (OptionSetValue)preImageEntity.Attributes[Enums.CaseEnums.CaseAttributes.CaseOriginCode.ToString().ToLower()];
                var ticketNumber = (string)preImageEntity.Attributes[Enums.CaseEnums.CaseAttributes.TicketNumber.ToString().ToLower()];
                var preStatusCode = (OptionSetValue)preImageEntity.Attributes[Enums.CaseEnums.CaseAttributes.StatusCode.ToString().ToLower()];
                var preStateCode = (OptionSetValue)preImageEntity.Attributes[Enums.CaseEnums.CaseAttributes.StateCode.ToString().ToLower()];
                var setToStatus = (OptionSetValue)context.InputParameters["Status"];

                if (caseOriginCode.Value == (int)Enums.CaseEnums.CaseOrigin.Monolith && preStatusCode.Value == (int)Enums.CaseEnums.StatusCode.Resolved && setToStatus.Value == (int)Enums.CaseEnums.StatusCode.PendingRadiant)
                {
                    localContext.Trace("Override status to cleared");
                    //SetStateResponse stateSet = (SetStateResponse)localContext.OrganizationService.Execute(stateRequest);
                    context.InputParameters["Status"] = new OptionSetValue((int)Enums.CaseEnums.StatusCode.Cleared);
                }
            }
        }
    }

    Tuesday, December 3, 2013 1:17 AM

All replies

  • Try to register your plugin on Pre-Validation stage.

    Hope this helps. If you get answer of your question, please mark the response as an answer and vote as helpful !!!
    Vikram Singh. !!!  My Blog

    Tuesday, December 3, 2013 1:49 AM