I just saw Nishant Rana's post about the auto-number data type that you can check here. And to be honest, I just knew about it and felt intrigued to test it. My expectation was pretty low about this data type (to handle lots of requests and result to duplicate numbers in the end). TL;DR (Too Long; Didn't Read), IT'S NOT!

For the testing purpose, I created the below Table with auto-number attribute with the name new_autonumber:

new_test and the auto-number attribute

You can create the auto-number attribute, using Auto Number Manager by Jonas Rapp (XrmToolbox Tool)/via new Solution UI (I'm using XrmToolbox):

Auto Number Manager by Jonas Rapp

For generating the entities inside the c# code, I used the below code:

 using System; using System.Collections.Generic; using System.Threading.Tasks; using System.Web.Configuration; using Microsoft.Xrm.Sdk; using Microsoft.Xrm.Tooling.Connector;  namespace CrmCheck {     class Program     {         static void Main(string[] args)         {             var connectionString = WebConfigurationManager.AppSettings["connectionString"];             var client = new CrmServiceClient(connectionString);              Console.WriteLine($"Connected to {client.OrganizationDetail.UniqueName}..");             var tasks = new List<Task>();             for (int i = 0; i < 100; i++)             {                 var number = $"Thread-{i.ToString().PadLeft(5, '0')}";                 tasks.Add(Task.Factory.StartNew(() =>                 {                     var id = Guid.NewGuid();                     var entity = new Entity("new_test");                     entity["new_name"] = number;                      Console.WriteLine($"Number {number}: {client.Create(entity)}");                 }));             }             Task.WaitAll(tasks.ToArray());             Console.ReadKey();         }     } } 

The code above, as you can see is using multithreading to create 100 new_test data per run. To make it more complex, I run 4 instances of the exe:

Run, run, run the exe

Using Deduplicator by Khoa Nguyen (XrmToolBox Tool) you can check the result if it will generate duplicate data or not:

No duplicate auto-number generated!

And voila! No duplicate data detected + It can handle multiple requests at the same time!

And psst, you also can re-use your string attribute + change it to the auto-number field via the new Solution UI that explained by Debaijit here!


This free site is ad-supported. Learn more