Skip to main content

Notifications

Microsoft Dynamics CRM (Archived)

Auto sequencer generation

Posted on by 795

Hello,

I have created a custom entity.I want to generate a +1 sequencer on each time create of New record.How it can be achieved using Plugins?

*This post is locked for comments

  • Suggested answer
    Abdul Wahab Profile Picture
    Abdul Wahab 12,070 Super User 2024 Season 1 on at
    RE: Auto sequencer generation

    Hi CRM Beginner,

    Please use this free solution crm2015autonumber.codeplex.com.

    Thank you

    Regards,

    AW

  • Aric Levin Profile Picture
    Aric Levin 30,188 on at
    RE: Auto sequencer generation

    Multiple users might be creating records at the same time.

    If your AutoNumbering logic becomes cumbersome, there are several AutoNumbering solutions out there, which are reasonable priced and some are even free.

  • CRM Beginner  Profile Picture
    CRM Beginner 795 on at
    RE: Auto sequencer generation

    Thanks for the reply Sir. But,i want to clear one thing that if i want to update the counter of records inside a single entity,why do we need lock entity?

  • Suggested answer
    tw0sh3ds Profile Picture
    tw0sh3ds 5,600 on at
    RE: Auto sequencer generation

    I once created a blog post about how to implement autonumbering using transactional nature of CRM plugins:

    https://bettercrm.blog/2017/05/11/making-use-of-transactions-in-dynamics-365-customer-engagement/

    Basically what you need to do is create a Counter entoty which will hold your last number and then

      1. Run plugin on create of numbered entity
      2. Retrieve Counter
      3. Update some dummy counter property to some dummy value (here we will lock the counter for usage only for this transaction)
      4. Retrieve Counter (now we are sure nobody will modify its value)
      5. Assign current value of counter to entity record
      6. Update counter with the next value
      7. Plugin ends (and any concurrent operations start from point 3)

    Some example code can look like this:

    namespace TransactionPlugin
    {
        public class Autonumber : IPlugin
        {
            public void Execute(IServiceProvider serviceProvider)
            {
                var pluginContext = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
                var factory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
                var service = factory.CreateOrganizationService(null);
    
                var target = pluginContext.InputParameters["Target"] as Entity;
    
                var qExpression = new QueryExpression("new_lock");
                qExpression.ColumnSet = new ColumnSet("new_name");
                qExpression.Criteria.AddCondition("new_name", ConditionOperator.Equal, "Counter");
                var results = service.RetrieveMultiple(qExpression);
                var counter = results.Entities.First();
                var blocker = new Entity("new_lock");
                blocker.Id = counter.Id;
                blocker["new_name"] = "Counter";
                service.Update(blocker); //NOW WE LOCK ALL TRANSACTIONS
    
                var lockedCounter = service.Retrieve("new_lock", blocker.Id, new ColumnSet("new_lastnumber"));
    
                var currentNumber = lockedCounter.GetAttributeValue<int>("new_lastnumber");
    
                target["accountnumber"] = $"{++currentNumber}";
    
                var counterUpdater = new Entity("new_lock");
                counterUpdater.Id = counter.Id;
                counterUpdater["new_lastnumber"] = currentNumber;
                service.Update(counterUpdater);
            }
        }
    }



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,240 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,149 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans