web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Duplicate Auto number

(0) ShareShare
ReportReport
Posted on by 70

I generate a record with the help of workflow in time out condition  on every month particular  date but  my auto number code create duplicate number. using lock in my code also not help me...

*This post is locked for comments

I have the same question (0)
  • Kokulan Profile Picture
    18,054 on at

    Could you please share the details about entity and the auto numbering tool/code you are using?

  • Suggested answer
    Rajesh Chungath Profile Picture
    467 on at

    Please make sure your lock object is static

    eg: private static readonly  object LockExecution = new object();

    lock (LockExecution)

               {

    //Auto number code

    }

  • arpit chouksey Profile Picture
    70 on at

    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 System.Threading;

    namespace ClientAutonumberID

    {

       public class Autonumber : IPlugin

       {

           DateTime Newinvoicegenerationdate;

           string Prefix, ID = "";

           int _invoiceID = 0;

           Entity ObjRegistration = new Entity();

           Entity ObjConfig = new Entity();

           static object locker = new object();

           public void Execute(IServiceProvider serviceProvider)

           {

               IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService((typeof(IPluginExecutionContext)));

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

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

               lock (locker)

               {

                   Thread.Sleep(1000);

                   Entity Invoiceobj = service.Retrieve(context.PrimaryEntityName, context.PrimaryEntityId, new ColumnSet(true));

                   QueryExpression Configuration_Q = new QueryExpression("new_configuration");

                   Configuration_Q.ColumnSet = new ColumnSet("new_invoiceprefix", "new_invoiceid");

                   EntityCollection ConfigurationColl = service.RetrieveMultiple(Configuration_Q);

                   if (ConfigurationColl.Entities.Count > 0)

                   {

                       ObjConfig = ConfigurationColl.Entities[0];

                       if (ObjConfig.Contains("new_invoiceprefix"))

                       {

                           Prefix = ObjConfig.GetAttributeValue<string>("new_invoiceprefix");

                       }

                       else

                           throw new Exception("Prefix is not Present");

                       if (ObjConfig.Contains("new_invoiceid"))

                       {

                           _invoiceID = ObjConfig.GetAttributeValue<Int32>("new_invoiceid");

                       }

                       else

                           throw new Exception("invoiceid is not Present");

                       string _idmonth = DateTime.Now.Month.ToString();

                       string _year = DateTime.Now.Year.ToString();

                       ID = Prefix + "/" + _year + "/" + _idmonth + "/" + _invoiceID.ToString();

                       _invoiceID++;

                       Invoiceobj.Attributes["new_name"] = ID;

                       ObjConfig.Attributes["new_invoiceid"] = _invoiceID;

                       Thread.Sleep(1000);

                       service.Update(Invoiceobj);

                       service.Update(ObjConfig);

                   }

               }

           }

       }

    }

  • Kokulan Profile Picture
    18,054 on at

    Could you please share what message this plugin is registered to?  Is it on Post Operation or Pre Validation?

  • arpit chouksey Profile Picture
    70 on at

    message: create

    On Post Operation and Try with Pre Validation but it give error :The requested record was not found or you do not have sufficient permissions to view it

  • Suggested answer
    Dynamics Objects 365 Profile Picture
    550 on at

    Hi Arpit,

    I think the problem is that you are using time out in condition in your Workflow. Auto number creation is one of the hardest thing to achieve.

    I think this can help you: powerapps.microsoft.com/.../autonumber-fields-are-now-supported-by-the-modern-entity-designer

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Meet the Microsoft Dynamics 365 Contact Center Champions

We are thrilled to have these Champions in our Community!

Congratulations to the April Top 10 Community Leaders

These are the community rock stars!

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
ScottDurow Profile Picture

ScottDurow 2

#2
GJones Profile Picture

GJones 1

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans