Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

How to write an auto-numbering plugin for a custom entity?

(0) ShareShare
ReportReport
Posted on by 360

I want to write a plugin for an entity called Students. I want to update a Student ID field automatically with auto numbers that increase by 1 each time, maybe with a prefix. So the Student ID field should = "SN" + AutoNumber.

I found a code that does this but with random numbering (in red below). I want it sequential autonumbers.

Can the system fetch the last created Student record, then add the "Student ID field + 1 to the new Student ID field in the new record? If so, can anyone suggest a code for this?


thanks in advance

***********************************
using System; using Microsoft.Xrm.Sdk; namespace Microsoft.Crm.Sdk.Samples { public class StudentIDPlugin: IPlugin { public void Execute(IServiceProvider serviceProvider) { // Obtain the execution context from the service provider. Microsoft.Xrm.Sdk.IPluginExecutionContext context = (Microsoft.Xrm.Sdk.IPluginExecutionContext) serviceProvider.GetService(typeof(Microsoft.Xrm.Sdk.IPluginExecutionContext)); // The InputParameters collection contains all the data passed in the message request. if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity) { // Obtain the target entity from the input parameters. Entity entity = (Entity)context.InputParameters["Target"]; // Verify that the target entity represents a Student. // If not, this plug-in was not registered correctly. if (entity.LogicalName == "Students") { // An Student ID attribute should not already exist because // it is system generated. if (entity.Attributes.Contains("StudentID") == false) { // Create a new Student ID attribute, set its value, and add // the attribute to the entity's attribute collection. Random rndgen = new Random(); entity.Attributes.Add("account name", rndgen.Next().ToString()); } else { // Throw an error, because Student ID must be system generated. // Throwing an InvalidPluginExecutionException will cause the error message // to be displayed in a dialog of the Web application. throw new InvalidPluginExecutionException("The student ID can only be set by the system."); } } } } } }

*************************

*This post is locked for comments

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: How to write an auto-numbering plugin for a custom entity?

    I have a plugin which generates an Autonumber by retrieving Count from a Custom Entity. My doubt is, what if hundred's of users jump on to the system and save at same time(with in seconds of time). My plugin takes time to retrieve the Count. So it's getting the previous count and that is causing identical Numbering. Is there a best way to implement this?

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: How to write an auto-numbering plugin for a custom entity?

    Hi Caty, I'm sorry. I didn't mean to be such a bother. So how's  your plugin. I m also looking over the net for this. If you dont mind kindly share its. Thanks

  • Saif Ehsan Profile Picture
    Saif Ehsan 360 on at
    RE: How to write an auto-numbering plugin for a custom entity?

    Thank you Enrik. The link is great, i just thought workflows can slow things down so i was hoping to use a plugin instead so I can also lock the field after update with less workflows.

  • Verified answer
    RaviKashyap Profile Picture
    RaviKashyap 55,410 on at
    RE: How to write an auto-numbering plugin for a custom entity?

    Hi Saif,

    There are already some free solution available for this. You can use this to achieve your requirement. You can configure the Prefix, Suffix and the last used number.

    crm2015autonumber.codeplex.com

    www.crmconsultants.co.uk/free-autonumber-solution-for-microsoft-dynamics-crm

    If you still want to create your own then you can refer the code from the codeplex for the above plugin

  • bebeben10 Profile Picture
    bebeben10 130 on at
    RE: How to write an auto-numbering plugin for a custom entity?

    Hi Saif,

    I suggest you store your auto number generator in a custom entity. Create all the necessary fields (prefix, number of digits, your auto number starts at, etc.) then create a record

    Upon creation of a Student record, retrieve the record you've just created for your auto number, concatenate all the necessary fields (prefix, number of digits, your auto number starts at, etc.) and then assign it to your Student Id field.

    After that, update your auto number record to increment it by 1.

  • Verified answer
    Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: How to write an auto-numbering plugin for a custom entity?

    Hi Saif,

    No need to write a code for this. You can actually do everything via workflow. See link below:

    http://www.encorebusiness.com/blog/dynamics-crm-auto-number-entities/

    I'm not the author of that post by the way so all credit goes to the author.

    Thanks,

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!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

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

#2
Martin Dráb Profile Picture

Martin Dráb 230,235 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans