Sequence Number/Auto Number for Custom Entity In MS CRM 2011

Generate Sequence Number/Auto Number for Custom Entity In MS CRM 2011

Create a Custom Entity:

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

Use the Below Plugin Code to Generate Sequence Number/Auto Number:

Write a plugin with this code and register on , for which entity you need Seq Number.

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Runtime.Serialization;

using Microsoft.Xrm.Sdk;

using Microsoft.Xrm.Client;

using Microsoft.Xrm.Sdk.Query;

namespace SequenceNumber

{

publicclassSequenceNumber : IPlugin

{

publicvoid 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”] isEntity)

{

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

if (entity.LogicalName == “new_request”)

{

QueryExpression expression = newQueryExpression(“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;

int requestTypeValue;

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 == “<FieldName From Autom Number Attribute Field>”)

{

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

entity.Attributes[attribute] = seqNumber;

}

if (attribute == “<FieldName From Autom Number Attribute Field>”)

{

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);

}

}

}

}

}

}

}

Happy Coding.

Advertisements

5 thoughts on “Sequence Number/Auto Number for Custom Entity In MS CRM 2011

  1. Hi :
    i have followed , each and every thing , but i have couple of dubt ,
    (i) if (attribute == “ “) , in this weather i have to mention auto number entity or entity in which auto number field.
    (II) In which entity i have to Register the the plugin , either in pre or post .
    (iii) while debugin the plugin i am getting the error, object reference not set to instance of variable , and while executing bellow line (” EntityCollection coll = service.RetrieveMultiple(expression); ” )the error occur , plz clarify my doubts

    • Hi,

      Please follow the procedure for creaating AutoNumber custom entity with all fields.
      (i) if (attribute == “ “) , “new_attribute” is a field present on AutoNumber entity which sholud refer the SeqNum/AutoNum schema name of a filed

      if (auto.Attributes.Contains(“new_startnumber”))
      {
      attribute = auto.Attributes[“new_attribute”].ToString();
      }
      if (attribute == “new_seqnumber”)//Here new_seqnumber is the schema name of a field which is present on the Entity Form where we need the SeqNum/AutoNum and that “new_seqnumber” is refer in the Attribute field on AutoNumber Custom Entity.
      {}
      (II) You have to Register the the plugin on where you want to genarate SeqNum/AutoNum with Pre-operation.

      (iii) Please follow the procedure accordingly , you wont get any errors.

      If you have any such type kindly post your code with error msg screen shot.

      Regards,
      Suresh Kumar D

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s