Simple Plugin Program For Trail

 1.)

using Microsoft.Xrm.Sdk;

using System;

using System.Collections.Generic;

using System.Linq;

using System.ServiceModel;

using System.Text;

using System.Threading.Tasks;


namespace ClassLibrary1

{

    public class Class1 : IPlugin

    {

        public void Execute(IServiceProvider serviceProvider)

        {


            ITracingService tracingService =

                 (ITracingService)serviceProvider.GetService(typeof(ITracingService));

            IPluginExecutionContext context = (IPluginExecutionContext)

                serviceProvider.GetService(typeof(IPluginExecutionContext));


            if (context.MessageName != "Create")


                return;


            if (context.InputParameters.Contains("Target") &&

                  context.InputParameters["Target"] is Entity)

            {

                Entity entity = (Entity)context.InputParameters["Target"];


                if (entity.LogicalName != "account")


                    return;

                IOrganizationServiceFactory serviceFactory =

                   (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));

                IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);


                try

                {


                    if (context.Stage == 40)

                    {

                        Entity updateAccount = new Entity("account");

                        updateAccount.Id = entity.Id;

                        updateAccount.Attributes["description"]= "Hello World ";

                        service.Update(updateAccount);

                    }

                }



                catch (Exception ex)

                {

                    tracingService.Trace("OnPostUpdate Plugin in the state entity : {0}", ex.ToString());

                }




            }

        }

    }

        

}



Note : As a developer we should minimize the service call above program you can also do in different way.

2).


 using Microsoft.Xrm.Sdk;

using System;

using System.Collections.Generic;

using System.Linq;

using System.ServiceModel;

using System.Text;

using System.Threading.Tasks;


namespace ClassLibrary1

{

    public class Class1 : IPlugin

    {

        public void Execute(IServiceProvider serviceProvider)

        {


            ITracingService tracingService =

                 (ITracingService)serviceProvider.GetService(typeof(ITracingService));

            IPluginExecutionContext context = (IPluginExecutionContext)

                serviceProvider.GetService(typeof(IPluginExecutionContext));


            if (context.MessageName != "Create")


                return;


            if (context.InputParameters.Contains("Target") &&

                  context.InputParameters["Target"] is Entity)

            {

                Entity entity = (Entity)context.InputParameters["Target"];


                if (entity.LogicalName != "account")


                    return;

                IOrganizationServiceFactory serviceFactory =

                   (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));

                IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);


                try

                {


                     if (context.Stage == 20)

                    {

                        

                        entity.Attributes.ad("description", "Hello World ");

                        service.Update(updateAccount);

                    }

                    

                }



                catch (Exception ex)

                {

                    tracingService.Trace("OnPostUpdate Plugin in the state entity : {0}", ex.ToString());

                }




            }

        }

    }

        

}

Comments

Popular posts from this blog

latest dynamics 365 interview questions for more than 2 years of experience

difference between enable rule vs display rule in ribbon workbench

If i create one record that created record date is older than 3 days, then I want to perform some operation with the help of business rule.