Plugin Codes For MS CRM

 


using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using Microsoft.Xrm.Sdk;

namespace CRM_Crate___My_Sample_Plugin

{

    public class Plugin : IPlugin

    {

        //Main Execute Method.

        public void Execute(IServiceProvider serviceProvider)

        {

            //Initializing Service Context.

            IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));

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

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

            try

            {

                //Defining Entity Object.

                Entity eTarget = null;

                if (context.MessageName == "Create")

                {

                    //Retrieving Target Entity.

                    eTarget = (context.InputParameters.Contains("Target") && context.InputParameters["Target"] != null) ?

                    context.InputParameters["Target"] as Entity : null;

                    //Triggering The Logic If Target Is Not Null.

                    if (eTarget != null)

                    {

                        //Reading Value From String Field.

                        string readField = eTarget.GetAttributeValue<string>("fieldschemaname");

                        //Setting Value In String Field.

                        eTarget.Attributes.Add("fieldschemaname", "Any Text Value");

                        //Sending Update Request.

                        service.Update(eTarget);

                    }

                }

            }

            catch (Exception ex)

            {

                throw new InvalidPluginExecutionException(ex.Message);

            }

        }

    }

}


//===================================

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using Microsoft.Xrm.Sdk;

namespace CRM_Crate___My_Sample_Plugin

{

    public class Plugin : IPlugin

    {

        //Main Execute Method.

        public void Execute(IServiceProvider serviceProvider)

        {

            //Initializing Service Context.

            IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));

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

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

            try

            {

                //Defining Entity Object.

                Entity eTarget = null;

                if (context.MessageName == "Create")

                {

                    //Retrieving Target Entity.

                    eTarget = (context.InputParameters.Contains("Target") && context.InputParameters["Target"] != null) ?

                    context.InputParameters["Target"] as Entity : null;

                    //Triggering The Logic If Target Is Not Null.

                    if (eTarget != null)

                    {

                        //Reading Value From Option Set Field.

                        int readField = eTarget.GetAttributeValue<OptionSetValue>("fieldschemaname").Value;

                        //Setting Value In Option Set Field.

                        eTarget.Attributes.Add("fieldschemaname", new OptionSetValue(100));

                        //Sending Update Request.

                        service.Update(eTarget);

                    }

                }

            }

            catch (Exception ex)

            {

                throw new InvalidPluginExecutionException(ex.Message);

            }

        }

    }

}



using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using Microsoft.Xrm.Sdk;

namespace CRM_Crate___My_Sample_Plugin

{

    public class Plugin : IPlugin

    {

        //Main Execute Method.

        public void Execute(IServiceProvider serviceProvider)

        {

            //Initializing Service Context.

            IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));

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

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

            try

            {

                //Defining Entity Object.

                Entity eTarget = null;

                if (context.MessageName == "Create")

                {

                    //Retrieving Target Entity.

                    eTarget = (context.InputParameters.Contains("Target") && context.InputParameters["Target"] != null) ?

                    context.InputParameters["Target"] as Entity : null;

                    //Triggering The Logic If Target Is Not Null.

                    if (eTarget != null)

                    {

                        //Reading Value From Whole Number Field.

                        int readField = eTarget.GetAttributeValue<int>("fieldschemaname");

                        //Setting Value In Whole Number Field.

                        eTarget.Attributes.Add("fieldschemaname", 100);

                        //Sending Update Request.

                        service.Update(eTarget);

                    }

                }

            }

            catch (Exception ex)

            {

                throw new InvalidPluginExecutionException(ex.Message);

            }

        }

    }

}


using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using Microsoft.Xrm.Sdk;

namespace CRM_Crate___My_Sample_Plugin

{

    public class Plugin : IPlugin

    {

        //Main Execute Method.

        public void Execute(IServiceProvider serviceProvider)

        {

            //Initializing Service Context.

            IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));

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

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

            try

            {

                //Defining Entity Object.

                Entity eTarget = null;

                if (context.MessageName == "Create")

                {

                    //Retrieving Target Entity.

                    eTarget = (context.InputParameters.Contains("Target") && context.InputParameters["Target"] != null) ?

                    context.InputParameters["Target"] as Entity : null;

                    //Triggering The Logic If Target Is Not Null.

                    if (eTarget != null)

                    {

                        //Reading Value From Lookup Field.

                        EntityReference readField = eTarget.GetAttributeValue<EntityRefrence>("fieldschemaname");

                        //Setting Value In Lookup Field.

                        Guid recordId = new Guid("8a2eab03-faa6-ea11-a812-000d3a8b66df");

                        eTarget.Attributes.Add("fieldschemaname", new EntityReference("entityschemaname", recordId));

                        //Sending Update Request.

                        service.Update(eTarget);

                    }

                }

            }

            catch (Exception ex)

            {

                throw new InvalidPluginExecutionException(ex.Message);

            }

        }

    }

}




using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using Microsoft.Xrm.Sdk;

namespace CRM_Crate___My_Sample_Plugin

{

    public class Plugin : IPlugin

    {

        //Main Execute Method.

        public void Execute(IServiceProvider serviceProvider)

        {

            //Initializing Service Context.

            IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));

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

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

            try

            {

                //Defining Entity Object.

                Entity eTarget = null;

                if (context.MessageName == "Create")

                {

                    //Retrieving Target Entity.

                    eTarget = (context.InputParameters.Contains("Target") && context.InputParameters["Target"] != null) ?

                    context.InputParameters["Target"] as Entity : null;

                    //Triggering The Logic If Target Is Not Null.

                    if (eTarget != null)

                    {

                        //Reading Value From DateTime Field.

                        DateTime readField = eTarget.GetAttributeValue<DateTime>("fieldschemaname");

                        //Setting Value In DateTime Field.

                        DateTime anyDateTime = DateTime.Now;

                        eTarget.Attributes.Add("fieldschemaname", anyDateTime);

                        //Sending Update Request.

                        service.Update(eTarget);

                    }

                }

            }

            catch (Exception ex)

            {

                throw new InvalidPluginExecutionException(ex.Message);

            }

        }

    }

}




using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using Microsoft.Xrm.Sdk;

namespace CRM_Crate___My_Sample_Plugin

{

    public class Plugin : IPlugin

    {

        //Main Execute Method.

        public void Execute(IServiceProvider serviceProvider)

        {

            //Initializing Service Context.

            IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));

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

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

            try

            {

                //Defining Entity Object.

                Entity eTarget = null;

                if (context.MessageName == "Create")

                {

                    //Retrieving Target Entity.

                    eTarget = (context.InputParameters.Contains("Target") && context.InputParameters["Target"] != null) ?

                    context.InputParameters["Target"] as Entity : null;

                    //Triggering The Logic If Target Is Not Null.

                    if (eTarget != null)

                    {

                        //Reading Value From Money Field.

                        Money readField = eTarget.GetAttributeValue<Money>("fieldschemaname");

                        Decimal readFieldInDecimal = eTarget.GetAttributeValue<Money>("fieldschemaname").Value;

                        //Setting Value In Money Field.

                        Money anyMoneyValue = new Money(1000);

                        eTarget.Attributes.Add("fieldschemaname", anyMoneyValue);

                        //Sending Update Request.

                        service.Update(eTarget);

                    }

                }

            }

            catch (Exception ex)

            {

                throw new InvalidPluginExecutionException(ex.Message);

            }

        }

    }

}



Comments

Popular posts from this blog

Power Automate - Update Child record when parent record is updated by using power automate in MS Dynamics CRM

Create Folder On Share Point by using Power Automate In Dynamics CRM

Duplicate Detection Plugin code for Microsoft dynamics CRM