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
Hi CRM Beginner,
Please use this free solution crm2015autonumber.codeplex.com.
Thank you
Regards,
AW
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.
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?
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
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); } } }
Stay up to date on forum activity by subscribing. You can also customize your in-app and email Notification settings across all subscriptions.
André Arnaud de Cal... 291,240 Super User 2024 Season 2
Martin Dráb 230,149 Most Valuable Professional
nmaenpaa 101,156