Skip to main content

Notifications

Announcements

No record found.

Dynamics 365 Community / Blogs / Adrian Begovich's Blog / Dynamics 365: C# Plugin Tha...

Dynamics 365: C# Plugin That Triggers When An Account Records Status Changes

Adrian Begovich Profile Picture Adrian Begovich 21,009 Super User 2024 Season 2

This is a C# plugin template for logic that triggers when an account records status changes.

using CrmEarlyBound;
using System;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;

namespace CRM.Plugins
/// 
/// A plugin that triggers when an account records status changes.
/// 
/// 
/// Register this plug-in:
/// Message: SetState & SetStateDynamicEntity
/// Primary Entity: account
/// Event Pipeline Stage: Pre-Operation
/// 
{
    public class Plugin : IPlugin
    {
        public void Execute(IServiceProvider serviceProvider)
        {
            ITracingService tracer = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
            IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
            IOrganizationServiceFactory factory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
            IOrganizationService service = factory.CreateOrganizationService(context.UserId);

            try
            {

                if (context.InputParameters.Contains("EntityMoniker") && context.InputParameters["EntityMoniker"] is EntityReference)
                {
                    EntityReference entityReference = (EntityReference)context.InputParameters["EntityMoniker"];
                    Entity entity = service.Retrieve(entityReference.LogicalName, entityReference.Id, new ColumnSet(true));
                    OptionSetValue state = (OptionSetValue)context.InputParameters["State"];
                    OptionSetValue status = (OptionSetValue)context.InputParameters["Status"];

                    //throw new InvalidPluginExecutionException(string.Format("Entity Name: {0}, State: {1}, Status: {2}", entityReference.LogicalName, state.Value.ToString(), status.Value.ToString()));

                    if (entity.LogicalName.Equals(Account.EntityLogicalName, StringComparison.OrdinalIgnoreCase))
                    {
                        AccountService accountSvc = new AccountService(entity.ToEntity(), tracer, service);

                        accountSvc.Method();
                    }
                }
            }
            catch (Exception e)
            {
                tracer.Trace(e.ToString());
                throw new InvalidPluginExecutionException(e.Message);
            }
        }
    }
}

using CrmEarlyBound;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Client;
using System;
using System.Linq;

namespace CRM.Plugins
{
    public class AccountService
    {
        private readonly Account account;
        private readonly ITracingService tracer;
        private readonly IOrganizationService service;

        public AccountService(Account account, ITracingService tracer, IOrganizationService service)
        {
            this.account = account;
            this.tracer = tracer;
            this.service = service;
        }

        public void Method()
        {
            tracer.Trace("Account Service: Method");
            if (account.AccountId != null)
            {
                using (OrganizationServiceContext orgSvcContext = new OrganizationServiceContext(service))
                {

                }
            }
        }
    }
}

Comments

*This post is locked for comments