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

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Not able to generate unique incremental numbers

(0) ShareShare
ReportReport
Posted on by

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

I have the same question (0)
  • Community Member Profile Picture
    on at

    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.

  • Suggested answer
    PS Profile Picture
    23,577 on at

    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

  • Inogic Profile Picture
    713 on at

    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
    vinod shivoor Profile Picture
    1,225 on at

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

                   }

               }

           }

       }

    }

  • Suggested answer
    vinod shivoor Profile Picture
    1,225 on at

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

  • tw0sh3ds Profile Picture
    5,600 on at

    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

  • Rao Rapolu Profile Picture
    470 on at

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

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…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans