There is no need to make any plugin or any external call for auto-numbering. Auto-numbering is now possible using just one attribute.
Things to do before getting into this.
- Should have one CRM instance. (D365 30-days free trial will do)
- Create Custom Entity (No extra fields are required except the primary one which is to be default).
That's it.
Note - We are not adding any field for auto number using CRM default solution.
So, until now, you should have one Entity created with no custom field in it (only default system generated fields). Now, it is time to write some code to see this auto-numbering thing happen.
What we have to do is to create an attribute using C# Code and make that attribute to be auto-numbered. We can even have some prefix or suffix also.
Jump to Visual Studio.
Create one console app.
First, add the reference to the following three NuGet packages.
Install the following two packages.
- Install-Package Microsoft.CrmSdk.CoreAssemblies -Version 9.0.0.5
- Install-Package Microsoft.CrmSdk.XrmTooling.CoreAssembly -Version 9.0.0.5
Add reference to System.configuration.dll.
Now, let us build and check if every package is installed properly. Add the following code to connect to CRM instance using C#.
- static void Main(string[] args)
- {
- CrmServiceClient crmServiceClientObj = new CrmServiceClient(ConfigurationManager.ConnectionStrings["CrmOnlineStringFromAppConfig"].ConnectionString);
- if (!crmServiceClientObj.IsReady) Console.WriteLine("No Connection was Made.");
- Console.WriteLine("Connected");
- }
To know more about connecting C# to CRM Online Instance, check out my blog Simple Code to connect to MS D365 Online using console Application.
Run the application to test if it is connected. You need to create two objects -
- StringAttributeMetadata to set attribute meta data
- CreateAttributeRequest which will hold entity name and attribute.
- private static string entityName = "new_autonumberdemoentity";
- static void Main(string[] args)
- {
- CrmServiceClient crmServiceClientObj = new CrmServiceClient(ConfigurationManager.ConnectionStrings["CrmOnlineStringFromAppConfig"].ConnectionString);
- if (!crmServiceClientObj.IsReady) Console.WriteLine("No Connection was Made.");
- Console.WriteLine("Connected");
- Console.WriteLine("Create Auto number Attribute for Entity {0}", entityName);
- var attributeMetaData = new StringAttributeMetadata()
- {
- AutoNumberFormat = "SYS {RANDSTRING:4} - ORG {SEQNUM:4}",
- SchemaName = "new_AutoNumAtt",
- RequiredLevel = new AttributeRequiredLevelManagedProperty(AttributeRequiredLevel.None),
- DisplayName = new Microsoft.Xrm.Sdk.Label("Entity Code", 1033),
- Description = new Microsoft.Xrm.Sdk.Label("The value will be AUTO GENERATED", 1033),
- IsAuditEnabled = new Microsoft.Xrm.Sdk.BooleanManagedProperty(false),
- IsGlobalFilterEnabled = new Microsoft.Xrm.Sdk.BooleanManagedProperty(true),
- MaxLength = 100
- };
- CreateAttributeRequest req = new CreateAttributeRequest()
- {
- EntityName = entityName,
- Attribute = attributeMetaData
- };
- crmServiceClientObj.Execute(req);
- Console.WriteLine("Created Auto number Attribute for Entity {0}", entityName);
- }
Before running the app, you will find no attribute. For this, a custom entity is present.
Apply the filter to the Views of Fields to custom.
Run the code and refresh the Field View to see.
Add this field on your form and publish it. Open the custom Entity and click new record to see a "Create" page which should have this attribute on the form but with no value in it.
Create a new record and save that. Boom! Check it out.
Set the View with a new custom attribute.
Check the following View.