Dynamics 365 CE – Set State using a plugin

Summary

Below is a code sample that demonstrates how to set the state of a record in Dynamics 365.

The plugin Step must be registered on both the SetState & SetStateDynamicEntity messages.

namespace AlphaTests.Plugins
{
    using System;
    using System.ServiceModel;
    using Microsoft.Xrm.Sdk;
    using Microsoft.Xrm.Sdk.Query;
    //-----------------------------------------------------------------------------------------------------
    public class PreOpportunitySetStateDynamicEntity : Plugin
    {
        //-----------------------------------------------------------------------------------------------------
        public PreOpportunitySetStateDynamicEntity()
            : base(typeof(PreOpportunitySetStateDynamicEntity))
        {
           // base.RegisteredEvents.Add(new Tuple<int, string, string, Action>(20, "Win", "opportunity", new Action(ExecutePreOpportunitySetStateDynamicEntity)));
            base.RegisteredEvents.Add(new Tuple<int, string, string, Action>(20, "SetState", "account", new Action(ExecutePreOpportunitySetStateDynamicEntity)));
            base.RegisteredEvents.Add(new Tuple<int, string, string, Action>(20, "SetStateDynamicEntity", "account", new Action(ExecutePreOpportunitySetStateDynamicEntity)));
        }
        //-----------------------------------------------------------------------------------------------------
        protected void ExecutePreOpportunitySetStateDynamicEntity(LocalPluginContext localContext)
        {
            if (localContext == null)
            {
                throw new ArgumentNullException("localContext");
            }

            IPluginExecutionContext context = localContext.PluginExecutionContext;
            IOrganizationService service = localContext.OrganizationService;
            EntityReference entity = null;

            if (context.InputParameters.Contains("EntityMoniker") && context.InputParameters["EntityMoniker"] is EntityReference)
            {
                entity = (EntityReference)context.InputParameters["EntityMoniker"];
            }
            else { return; }

            try
            {
                Entity currentRecord = new Entity();

                ColumnSet attributes = new ColumnSet(new string[] { "name" });
                currentRecord = service.Retrieve(entity.LogicalName, entity.Id, attributes);

                string topic = currentRecord["name"] as string;

                topic = topic + " Status Changed";

                currentRecord["name"] = topic;

                service.Update(currentRecord);
            }
            catch (FaultException ex)
            {
                throw new InvalidPluginExecutionException("An error occurred in the plug-in", ex);
            }
        }
    }
}
namespace AlphaTests.Plugins
{
    using System;
    using System.ServiceModel;
    using Microsoft.Xrm.Sdk;
    using Microsoft.Xrm.Sdk.Query;
    //-----------------------------------------------------------------------------------------------------
    public class PreOpportunitySetStateDynamicEntity : Plugin
    {
        //-----------------------------------------------------------------------------------------------------
        public PreOpportunitySetStateDynamicEntity()
            : base(typeof(PreOpportunitySetStateDynamicEntity))
        {
           // base.RegisteredEvents.Add(new Tuple<int, string, string, Action>(20, "Win", "opportunity", new Action(ExecutePreOpportunitySetStateDynamicEntity)));
            base.RegisteredEvents.Add(new Tuple<int, string, string, Action>(20, "SetState", "account", new Action(ExecutePreOpportunitySetStateDynamicEntity)));
            base.RegisteredEvents.Add(new Tuple<int, string, string, Action>(20, "SetStateDynamicEntity", "account", new Action(ExecutePreOpportunitySetStateDynamicEntity)));
        }
        //-----------------------------------------------------------------------------------------------------
        protected void ExecutePreOpportunitySetStateDynamicEntity(LocalPluginContext localContext)
        {
            if (localContext == null)
            {
                throw new ArgumentNullException("localContext");
            }

            IPluginExecutionContext context = localContext.PluginExecutionContext;
            IOrganizationService service = localContext.OrganizationService;
            EntityReference entity = null;

            if (context.InputParameters.Contains("EntityMoniker") && context.InputParameters["EntityMoniker"] is EntityReference)
            {
                entity = (EntityReference)context.InputParameters["EntityMoniker"];
            }
            else { return; }

            try
            {
                Entity currentRecord = new Entity();

                ColumnSet attributes = new ColumnSet(new string[] { "name" });
                currentRecord = service.Retrieve(entity.LogicalName, entity.Id, attributes);

                string topic = currentRecord["name"] as string;

                topic = topic + " Status Changed";

                currentRecord["name"] = topic;

                service.Update(currentRecord);
            }
            catch (FaultException ex)
            {
                throw new InvalidPluginExecutionException("An error occurred in the plug-in", ex);
            }
        }
    }
}