Auto Numbering For Custom Entity In Dynamic CRM

There is no need to make any plugin or any external call for auto-numbering. Auto-numbering is now possible using just one attribute.

Things to do before getting into this.

  1. Should have one CRM instance. (D365 30-days free trial will do)
  2. Create Custom Entity (No extra fields are required except the primary one which is to be default).

That's it.

Note - We are not adding any field for auto number using CRM default solution.

So, until now, you should have one Entity created with no custom field in it (only default system generated fields). Now, it is time to write some code to see this auto-numbering thing happen.

What we have to do is to create an attribute using C# Code and make that attribute to be auto-numbered. We can even have some prefix or suffix also.

Jump to Visual Studio. 

Create one console app.

First, add the reference to the following three NuGet packages.

Dynamic CRM

Dynamic CRM

Install the following two packages.

  • Install-Package Microsoft.CrmSdk.CoreAssemblies -Version 9.0.0.5
  • Install-Package Microsoft.CrmSdk.XrmTooling.CoreAssembly -Version 9.0.0.5 

Add reference to System.configuration.dll.

Dynamic CRM

Now, let us build and check if every package is installed properly. Add the following code to connect to CRM instance using C#.

  1. static void Main(string[] args)   
  2. {  
  3.     CrmServiceClient crmServiceClientObj = new CrmServiceClient(ConfigurationManager.ConnectionStrings["CrmOnlineStringFromAppConfig"].ConnectionString);  
  4.     if (!crmServiceClientObj.IsReady) Console.WriteLine("No Connection was Made.");  
  5.     Console.WriteLine("Connected");  
  6. }  

To know more about connecting C# to CRM Online Instance, check out my blog Simple Code to connect to MS D365 Online using console Application.

Run the application to test if it is connected. You need to create two objects -

  1. StringAttributeMetadata to set attribute meta data
  2. CreateAttributeRequest which will hold entity name and attribute.
  1. private static string entityName = "new_autonumberdemoentity";  
  2. static void Main(string[] args)   
  3. {  
  4.     CrmServiceClient crmServiceClientObj = new CrmServiceClient(ConfigurationManager.ConnectionStrings["CrmOnlineStringFromAppConfig"].ConnectionString);  
  5.     if (!crmServiceClientObj.IsReady) Console.WriteLine("No Connection was Made.");  
  6.     Console.WriteLine("Connected");  
  7.     Console.WriteLine("Create Auto number Attribute for Entity {0}", entityName);  
  8.     var attributeMetaData = new StringAttributeMetadata()   
  9.     {  
  10.         AutoNumberFormat = "SYS {RANDSTRING:4} - ORG {SEQNUM:4}"//{DATETIMEUTC:yyyyMMddhhmmss} can also be used  
  11.             SchemaName = "new_AutoNumAtt"//this should be unique  
  12.             RequiredLevel = new AttributeRequiredLevelManagedProperty(AttributeRequiredLevel.None), //set it as per required  
  13.             DisplayName = new Microsoft.Xrm.Sdk.Label("Entity Code", 1033), //Lable Name  
  14.             Description = new Microsoft.Xrm.Sdk.Label("The value will be AUTO GENERATED", 1033), // On hover description  
  15.             IsAuditEnabled = new Microsoft.Xrm.Sdk.BooleanManagedProperty(false),  
  16.             IsGlobalFilterEnabled = new Microsoft.Xrm.Sdk.BooleanManagedProperty(true), // we need it to be searched direclty from global search.  
  17.             MaxLength = 100 //  
  18.     };  
  19.     CreateAttributeRequest req = new CreateAttributeRequest()  
  20.     {  
  21.         EntityName = entityName,  
  22.             Attribute = attributeMetaData  
  23.     };  
  24.     crmServiceClientObj.Execute(req);  
  25.     Console.WriteLine("Created Auto number Attribute for Entity {0}", entityName);  
  26. }  

Before running the app, you will find no attribute. For this, a custom entity is present.

Apply the filter to the Views of Fields to custom.

Dynamic CRM 



Run the code and refresh the Field View to see.

Dynamic CRM 

Add this field on your form and publish it. Open the custom Entity and click new record to see a "Create" page which should have this attribute on the form but with no value in it.

Dynamic CRM

Create a new record and save that. Boom! Check it out.

Dynamic CRM

Set the View with a new custom attribute.

Check the following View.

Dynamic CRM


Similar Articles