Skip to main content

Auto Number Generation MS CRM 2016,2013,2011 through plugin C#

In this article I am showing you how you can generate number sequence of a specific record of specific entity.
here are the step

Step:1

    First of all create an Entity "new_autonumber"

Step:2

after that you will need to create the fields.
new_entityattributename
(field name which you need to use as primary column of Entity .Mostly new_name are the primary attribute in ms crm)

new_number
(you don't need to add anything here. this field will show how much number record created of specific entity)

new_prefix
(this field use to prefix for number sequence)

new_typename
(entity name will be set in this field)


Step:3

Set these fields on form and views


Step:4

download my plugin from github.
https://github.com/rrizwann/DynamicsCRM
download AutoNumberGeneration folder

Code are shown here.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
using Microsoft.Xrm.Sdk.Messages;

namespace AutoNumberGeneration
{
    public class AutoNumber : IPlugin
    {
        public void Execute(IServiceProvider serviceProvider)
        {
            IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
            int autoNumber = 0;
            string prefix = string.Empty;
            string suffix = string.Empty;
            if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
                {
                Entity entity = (Entity)context.InputParameters["Target"];
                IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
                IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);

                if (context.MessageName == "Create")
                {
                    #region On Create
                    QueryExpression query = new QueryExpression("new_autonumber");
                    FilterExpression childFilter = query.Criteria.AddFilter(LogicalOperator.And);
                    childFilter.AddCondition("new_typename", ConditionOperator.Equal, entity.LogicalName);
                    query.ColumnSet = new Microsoft.Xrm.Sdk.Query.ColumnSet("new_typename", "new_autonumberid", "new_prefix", "new_number", "new_suffix", "new_entityattributename");
                    #endregion
            
                    EntityCollection entitycollection = service.RetrieveMultiple(query);
                    if (entitycollection.Entities.Count > 0)
                    {
                        Entity _dynamicsEntity = (Entity)entitycollection.Entities[0];
                        if (_dynamicsEntity.Attributes.Contains("new_number"))
                        {
                            autoNumber =Convert.ToInt32( _dynamicsEntity.Attributes["new_number"]) + 1;
                        }

                        #region Prefix Handling

                        if (_dynamicsEntity.Attributes.Contains("new_prefix"))
                            {
                                prefix = _dynamicsEntity.Attributes["new_prefix"].ToString();
                            }
                          
                         #endregion

                        #region Updating Autonumber

                        if (_dynamicsEntity.Attributes.Contains("new_entityattributename"))
                        {
                            string _attributeName = _dynamicsEntity.Attributes["new_entityattributename"].ToString();

                            string _formatedAutoNumber = prefix + autoNumber.ToString().PadLeft(4, '0');

                            _dynamicsEntity.Attributes["new_prefix"] = prefix;

                            _dynamicsEntity.Attributes["new_number"] = autoNumber;
                           
                            entity.Attributes[_attributeName] = _formatedAutoNumber;
                           
                            service.Update(_dynamicsEntity);
                        }

                        #endregion
                    }
                }
            }
          }
        }
    }

Step:5

Load assembly in plugin registration tool

Step:6

You have to add step for rendering the plugin
I have set onCreate event

Step:7

Now go to CRM and configure the Auto Number entity.
I have configured in different entity.


Step:8
Create new record and configured it

Step:9

once you save the record in Entity
you will see in Autogenerated ID in Primary field.

Comments