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

Community site session details

Session Id :
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

I have the same question (0)
  • Verified answer
    Community Member Profile Picture
    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,

  • bebeben10 Profile Picture
    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
    RaviKashyap Profile Picture
    55,410 Moderator 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

  • Saif Ehsan Profile Picture
    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.

  • Community Member Profile Picture
    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

  • Community Member Profile Picture
    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?

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…

Abhilash Warrier – Community Spotlight

We are honored to recognize Abhilash Warrier as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
HR-09070029-0 Profile Picture

HR-09070029-0 2

#1
UllrSki Profile Picture

UllrSki 2

#3
ED-30091530-0 Profile Picture

ED-30091530-0 1

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans