Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

Duplicate Auto number

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

  • Suggested answer
    RE: Duplicate Auto number

    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

  • arpit chouksey Profile Picture
    arpit chouksey 70 on at
    RE: Duplicate Auto number

    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

  • Kokulan Profile Picture
    Kokulan 18,048 on at
    RE: Duplicate Auto number

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

  • arpit chouksey Profile Picture
    arpit chouksey 70 on at
    RE: Duplicate Auto number

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

                   }

               }

           }

       }

    }

  • Suggested answer
    Rajesh Chungath Profile Picture
    Rajesh Chungath 465 on at
    RE: Duplicate Auto number

    Please make sure your lock object is static

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

    lock (LockExecution)

               {

    //Auto number code

    }

  • Kokulan Profile Picture
    Kokulan 18,048 on at
    RE: Duplicate Auto number

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

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,280 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,214 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans