Auto Number Id Generation for Any Entity using Plugin

 In Microsoft dynamics, we have auto generated id for the case, quote and for some other. Like that if u want to generate any auto generate id then follow this post, you can achieve this easily. 

Before going this post, you must have idea on shared variables and also crud operation by using plugin.

We will discuss detailly.

For the auto number generation, we required one backup entity to store the number. and one more entity to generate id. 

In My Scenario, I am using opportunity entity for id generation and configuration for the backup entity.

I am triggering my plugin on creation of opportunity entity. If any record is created at that time I need to generate id for the each record without duplication. I am creating two plugins for my scenario.


Step1: Add the one class library project in visual studio 

Step2: Install the nugget packages for the project like crm. sdk 

Step: Add the common code to plugin.

Follow the below code for the auto generation

 using Microsoft.Crm.Sdk.Messages;

using Microsoft.Xrm.Sdk;

using System;

using Microsoft.Xrm.Sdk.Workflow;

using System.Activities;

using CodeActivity = System.Activities.CodeActivity;

using Microsoft.Xrm.Sdk.Query;


namespace Week2plugins

{

 public   class AutogenateId :IPlugin

    {

        public void Execute(IServiceProvider serviceProvider)

        {

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

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

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

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

            if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)

            {

                try

                {

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

// i am passing the my backup entity record guid. Because we need to store the number what was //garneted before.

                    Guid configid = new Guid("146305cb-d013-ee11-9cbe-6045bda56711");

// i am retrving the my backup entity data by passing the guid.

                    Entity confienity = service.Retrieve("msdynmkt_configuration", configid, new ColumnSet("msdynmkt_name"));

//getting the data present in my filed. i am storing my filed in string type, so i am taking name

                    string sourcenumber = confienity.GetAttributeValue<string>("msdynmkt_name");

                    tracing.Trace(sourcenumber);

//i am converting to integer and incrementing the the value

                    int autonumber = 1 + int.Parse(sourcenumber);

//you can use the target object . i am adding the some prefix and my above number       

                    Opparchunitydata["new_opportunityid"] = "opp" + "-" + auto number;

// i am settign the value for the shared variable with key and value

                    context.SharedVariables["sharenumber"] = autonumber;

                }

                catch (Exception ex)

                {

                    throw new InvalidPluginExecutionException(ex.Message);

                }

            }

        }

    }

}

 By using above code we can generate id for the record.

 For the storing the generated number we need to wrtie one plugin with create of opportunity.

Below code to store the value

using Microsoft.Crm.Sdk.Messages;

using Microsoft.Xrm.Sdk;

using System;

using Microsoft.Xrm.Sdk.Workflow;

using System.Activities;

using CodeActivity = System.Activities.CodeActivity;

using Microsoft.Xrm.Sdk.Query;


namespace Week2plugins

{

   public class Updateautonumber :IPlugin

    {

        public void Execute(IServiceProvider serviceProvider)

        {

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

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

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

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

            if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)

            {

                try

                {

// i am passing the same guid what we need in above code

                    Guid configid = new Guid("146305cb-d013-ee11-9cbe-6045bda56711");

//reading the shared varable value 

                    string sharedValue = context.SharedVariables["sharenumber"].ToString();

                    tracing.Trace(sharedValue);

//i am retrving the data by passing the guid

                    Entity Configudata = service.Retrieve("msdynmkt_configuration", configid, new ColumnSet("msdynmkt_name"));

// reading the filed data what was present before

                    string sourcenumber = Configudata.GetAttributeValue<string>("msdynmkt_name");

                    if (sourcenumber != null)

                    {

// updating the number by using shared variables

                        Configudata["msdynmkt_name"] = sharedValue;

                    }

                    service.Update(Configudata);

                    tracing.Trace("updated");

                    return;

                }

                catch (Exception EX)

                {

                    throw new InvalidPluginExecutionException(EX.Message);

                }

            }

        }

    }

}

Register the plugin on creation target entity with pre operation as pipeline

After executing the you can get like below


This is my backup entity data. it will store what was last garneted number


Thanks for reading my post and follow the page for the latest posts





Comments

Popular posts from this blog

Connect MS Crm To Postman For Webapi Calls.

create HTML page show all contact related to specific account, add this webresource as a subgrid in the form

create one PCF control and embed into crm