Dynamics 365





Tuesday, December 16, 2014

Sequence Number Generator for Custom Entity In MS CRM 2011



Sequence Number Generator for Custom Entity In MS CRM 2011
Create a custom entity to store starting number , Max number(last generated number) and prefix.
Physical Name: Auto Number , Logical Name: new_autonumber
Create the folloing Attributes:
PhysicalName : EntityLogName, Logical Name : new_EntityLogName DType :SingleLine
PhysicalName:StartNumber, Logical Name : new_ StartNumber DType :INT
PhysicalName:LastAllocatedNumber, Logical Name: new_LastAllocatedNumber DType :INT
PhysicalName:Prefix, Logical Name: new_Prefix, DType :Single Line
PhysicalName:Suffix, Logical Name: new_Suffix DType :Single Line
PhysicalName:Seperator, Logical Name: new_Seperator DType :Single Line
PhysicalName:Attribute, Logical Name: new_Attribute DType :Single Line

Once Cutsom Entity created Add the fields to form of custom entity .
It should look like below.
new_seqnumber   This filed should be there in lead  form(Create new_seqnumber Filed in lead form)
Name is on which entity you want to generate auto sequence number.
PreFix  : you can pass any value if you want to give prefix to number.
lastAllocatedNumber is max number or last generated number of lead entity.




Here is the Plugin code to generate auto seq number .
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Client;
using Microsoft.Xrm.Sdk.Query;
namespace AutoSeqNumber
{
    public class GenAutoSql:IPlugin
    {
        public void Execute(IServiceProvider serviceProvider)
        {

            IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(Microsoft.Xrm.Sdk.IPluginExecutionContext));

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

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

            if (context.Depth == 1 && context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
            {

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

                if (entity.LogicalName == "lead")
                {
                    try
                    {
                        QueryExpression expression = new QueryExpression("new_autonumber");

                        expression.ColumnSet.AllColumns = true;

                        expression.Criteria.AddCondition("new_name", ConditionOperator.Equal, entity.LogicalName);

                        EntityCollection coll = service.RetrieveMultiple(expression);

                        foreach (Entity auto in coll.Entities)
                        {

                            if (coll != null && coll.Entities.Count > 0)
                            {

                                int startNumber = 0;

                                int number;

                                string prefix = string.Empty; ;

                                string suffix = string.Empty;

                                string seperator = string.Empty;

                                string attribute = string.Empty;

                                int lastAllocated;

                                string seqNumber = string.Empty;

                                string requestType = string.Empty;                               

                                DateTime today = DateTime.Now;

                                if (auto.Attributes.Contains("new_startnumber"))
                                {

                                    startNumber = (int)auto.Attributes["new_startnumber"];

                                }

                                if (auto.Attributes.Contains("new_lastallocatednumber"))
                                {

                                    number = (int)auto.Attributes["new_lastallocatednumber"];

                                    lastAllocated = number;

                                    lastAllocated++;

                                }

                                else
                                {

                                    number = startNumber;

                                    lastAllocated = number;

                                }

                                if (auto.Attributes.Contains("new_prefix"))
                                {

                                    prefix = auto.Attributes["new_prefix"].ToString();

                                }

                                if (auto.Attributes.Contains("new_suffix"))
                                {

                                    suffix = auto.Attributes["new_suffix"].ToString();

                                }

                                if (auto.Attributes.Contains("new_seperator"))
                                {

                                    seperator = auto.Attributes["new_seperator"].ToString();

                                }

                                if (auto.Attributes.Contains("new_startnumber"))
                                {

                                    attribute = auto.Attributes["new_attribute"].ToString();

                                }

                                if (attribute == "new_seqnumber")
                                {

                                    seqNumber = prefix + seperator + lastAllocated.ToString();

                                    entity.Attributes[attribute] = seqNumber;

                                }

                                if (attribute == "new_seqnumber")
                                {

                                    seqNumber = prefix + seperator + lastAllocated.ToString() + seperator + suffix;

                                    entity.Attributes[attribute] = seqNumber;

                                }

                                seqNumber = prefix + seperator + lastAllocated.ToString() + seperator + suffix;

                                auto.Attributes["new_lastallocatednumber"] = lastAllocated;

                                service.Update(auto);

                            }

                        }
                    }
                    catch (Exception ex)
                    {
                        throw new InvalidPluginExecutionException(
                           "An error occurred in the Generating Sequence Number" + ex.Message, ex);
                    }

                }
                else
                {
                    throw new InvalidPluginExecutionException(
                                       "Seq number should be on lead");
                }

            }
          

        }
    }
}


Once Pulg in has been completed register assemble .
And add step on pre-Operation.
Now create new Lead it will generate seq number .