Skip to main content

Notifications

Microsoft Dynamics CRM (Archived)

Not able to generate unique incremental numbers

Posted on by Microsoft Employee

Hi Below is what I tried but every time CRM is creating multiple records with duplicate numbers:

1st Way:

1. Keep counter on a separate entity record.

2. Update the lookup of counter record on the main record.

3. Increment the counter by 1.

4. Update the new number on main record.

5. Stop.

2nd Way:

1. Keep counter on a separate entity record.

2. Update the lookup of counter record on the main record.

3. Update the new number on main record.

4. Increment the counter by 1.

5. Stop.

I have also tried to do it through code and making a false update on counter record so that CRM can apply the lock until the activity is complete. But again, it creates records with duplicate numbers. Can anyone help me out here?

*This post is locked for comments

  • Rao Rapolu Profile Picture
    Rao Rapolu 470 on at
    RE: Not able to generate unique incremental numbers

    yes real time workflow will do the trick when you run in organization context

  • tw0sh3ds Profile Picture
    tw0sh3ds 5,600 on at
    RE: Not able to generate unique incremental numbers

    Hi,

    If this is Online then you can never prepare a bullet-proof autonumbering in plugins, because you can't be sure if you are using only one application server. The best you can do, and I did not see you doing (or anybody suggesting you to do so), is to put lock in the beginning of your execute method. So you should have something like:

    public object _sync = new object()
    
    public void Execute
    {
       lock(_sync)
       {
          //all your code here
       }
    }

    This would make sure you have unique numbers if there is only one IIS worker for your CRM. You cannot be sure of that in Online deployment, unfortunately.

    Pawel

  • Suggested answer
    vinod shivoor Profile Picture
    vinod shivoor 1,225 on at
    RE: Not able to generate unique incremental numbers

    Try the above coded make your changes as needed. register the plugin in pre operation

  • Suggested answer
    vinod shivoor Profile Picture
    vinod shivoor 1,225 on at
    RE: Not able to generate unique incremental numbers

    using System;

    using System.Collections.Generic;

    using System.Linq;

    using System.Text;

    using Microsoft.Xrm.Sdk;

    using Microsoft.Xrm.Sdk.Query;

    using Microsoft.Xrm.Sdk.Client;

    using System.ServiceModel;

    namespace autonumber

    {

       public class Autono : IPlugin

       {

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

               string seq = "";

               int lastnumber;

               if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)

               {

                   Entity entity = context.InputParameters["Target"] as Entity;

                   string records = @"

                                    <fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>

                                    <entity name='new_autonumber'>

                                    <attribute name='new_lastnumber1'/>

                                    </entity>

                                    </fetch> ";

                   EntityCollection result = _service.RetrieveMultiple(new FetchExpression(records));

                   foreach (var c in result.Entities)

                   {

                       string count = (string)c.Attributes["new_lastnumber1"];

                       lastnumber = Convert.ToInt16(count);

                       lastnumber = lastnumber + 1;

                       seq = lastnumber.ToString();

                       c["new_lastnumber1"] = seq;

                       _service.Update(c);

                   }

                   Entity e = context.InputParameters["Target"] as Entity;

                   string zoneoptionSetValue = e.FormattedValues["new_zone"].ToString();

                   string zone = (zoneoptionSetValue.Substring(0, 2)).ToUpper();    

                   string wsceoptionSetValue = e.FormattedValues["new_wsccategory"].ToString();

                   string wsc = (wsceoptionSetValue.Substring(0, 2)).ToUpper();

                   int len = seq.Length;

                   if (len == 1)

                   {

                       seq = zone + wsc + 0 + 0 + 0 + 0 + seq;

                       e["new_accountnumber"] = seq;

                       _service.Update(e);

                   }

                   if (len == 2)

                   {

                       seq = zone + wsc + 0 + 0 + 0 + seq;

                       e["new_accountnumber"] = seq;

                       _service.Update(e);

                   }

                   if (len == 3)

                   {

                       seq = zone + wsc + 0 + 0 + seq;

                       e["new_accountnumber"] = seq;

                       _service.Update(e);

                   }

                   if (len == 4)

                   {

                       seq = zone + wsc + 0 + seq;

                       e["new_accountnumber"] = seq;

                       _service.Update(e);

                   }

                   if (len == 5)

                   {

                       seq = zone + wsc + seq;

                       e["new_accountnumber"] = seq;

                       _service.Update(e);

                   }

               }

           }

       }

    }

  • Inogic Profile Picture
    Inogic 24,094 on at
    RE: Not able to generate unique incremental numbers

    Please try with below solution.

    • Write a synchronous Pre plugin on update.
    • At the start of plugin update another entity for counter.
    • Set that counter in target field and attach to context.

     Hope this helps.

  • Suggested answer
    PS Profile Picture
    PS 23,577 on at
    RE: Not able to generate unique incremental numbers

    Following two solutions can help you:

    1. Auto-numbering via workflows: prashantshuklacrm.wordpress.com/.../auto-numbering-through-real-time-workflows-no-customization-no-add-on

    2. Auto-Numbering, third party: www.planetxrm.com/microsoft-dynamics-crm-2013-2011-auto-id-generator

    This third party solution is tried and tested and does not give a duplicate ever

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Not able to generate unique incremental numbers

    Hi, thanks for the answer, but I used 3rd party solutions in the first place but because it was generating duplicate ids so I had to shift to my own solution.

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!

Community AMA December 12th

Join us as we continue to demystify the Dynamics 365 Contact Center

Leaderboard

#1
André Arnaud de Calavon Profile Picture

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

#2
Martin Dráb Profile Picture

Martin Dráb 230,188 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans