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

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Auto Number generation

(0) ShareShare
ReportReport
Posted on by 2,510

Hi, when I do auto number generation in plugin, I use "lock (SyncLock)" to handle the concurrency, but it looks like it doesn't work very well, I still get duplicated numbers sometimes. 

 private void RegNumber(Entity entity, IOrganizationService service)
 {
     lock (SyncLock)
     {
  if (entity != null)
  {
      // read  counter
      // update counter
  }
     }
  }


*This post is locked for comments

I have the same question (0)
  • Suggested answer
    Radu Chiribelea Profile Picture
    6,667 on at
    RE: Auto Number generation

    Hello - please check if following blog can help you with your request : www.akaes.com/.../how-to-implement-robust-auto-numbering-using-transactions-in-microsoft-dynamics-crm

  • Suggested answer
    Community Member Profile Picture
    on at
    RE: Auto Number generation

    Hi sdnd2000 ,

    you need to set your plugin like following code:

    namespace YOURNAMESPACE.Plugins
    {
        public class MyAutoNumber : IPlugin
        {
            public MyAutoNumber()
            {
            }
    
            private static readonly object SyncLock = new object();
    
            public void Execute(IServiceProvider serviceProvider)
            {
                var context = (Microsoft.Xrm.Sdk.IPluginExecutionContext)serviceProvider.GetService(typeof(Microsoft.Xrm.Sdk.IPluginExecutionContext));
                var factory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
                var service = factory.CreateOrganizationService(context.UserId);
                var orgcontext = new OrganizationServiceContext(service);
                var tracer = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
    
                try
                {
                    if (context.InputParameters.Contains("Target") || (context.InputParameters["Target"] is Entity))
                    {
                        var entity = context.InputParameters["Target"] as Entity;
    
                        if (context.IsInTransaction)
                        {
                            lock (SyncLock)
                            {
    			  // your code here
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    tracer.Trace("MyAutoNumber Plugin Exception: " + ex);
                    throw new InvalidPluginExecutionException(ex.Message);
                }
            }
        }
    }

    Please let me know if you solve.

    If you found the answer helpful, please mark as Verified 

    Join my network on LinkedIn      Follow me on Twitter 

    Thank You & Best Regards

    Francesco Picchi

    Microsoft Dynamics CRM Consultant, Bologna+Milano, ITALY

    Independent Contractor

    http://www.francescopicchi.com

  • Wayne Walton Profile Picture
    13,728 on at
    RE: Auto Number generation

    There are a great many free (and some for-pay) options out there for autonumbering in CRM.  you might be better off just using one someone has already created instead.

  • sdnd2000 Profile Picture
    2,510 on at
    RE: Auto Number generation

    Hi, Francesco,

    sorry, I didn't paste all the code, but your code snippet is exactly the same as what I am using nows

  • Verified answer
    tw0sh3ds Profile Picture
    5,600 on at
    RE: Auto Number generation

    Locking is not good enough if your plugins are running in Sandbox (there can be multiple Sandbox processing services). The correct way (with some exceptions but usually works without a problem) can be found here:

    bettercrm.blog/.../making-use-of-transactions-in-dynamics-365-customer-engagement

    Basically what you have to do is to create a dummy update to lock the transaction and do all the critical section logic after that.

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Abhilash Warrier – Community Spotlight

We are honored to recognize Abhilash Warrier as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
Community Member Profile Picture

Community Member 2

#1
UllrSki Profile Picture

UllrSki 2

#3
SC-08081331-0 Profile Picture

SC-08081331-0 1

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans