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