Auto Number field was the long-awaited feature in Dynamics CRM. Earlier in CRM auto-number feature was supported for some of the limited OOB CRM entities (ex: Case, Quotes, Order etc.). I still remember earlier if we had to implement Auto number feature for a custom entity, we generally used to build a custom solution for this.
From Dynamics 365 for Customer Engagement App 9.0 version, Auto number feature is an in-house OOB feature & now we can generate Auto Number for custom and OOB entities, however currently Microsoft has not given any UI to create an Auto number field directly, however, in this blog I will demonstrate how we can create auto number field programmatically and leverage the SQL cluster indexing.
Note: As per Microsoft in future they will also provide CRM interface to directly create auto number field in database.
Below is the C# .Net code though which you can create Auto Number programmatically. In the below code I am createing a custom auto number field on Account entity.
Namespace
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Messages;
using Microsoft.Xrm.Sdk.Metadata;
Method
public void Create_AutoNumberField(IOrganizationService service)
{
try
{
CreateAttributeRequest widgetSerialNumberAttributeRequest = new CreateAttributeRequest
{
EntityName = "account",
Attribute = new StringAttributeMetadata
{
//Define the format of the attribute
AutoNumberFormat = "ACC-{SEQNUM:4}-{RANDSTRING:4}-{DATETIMEUTC:yyyyMMddhhmmss}",
LogicalName = "new_accountnumber",
SchemaName = "new_AccountNumber",
RequiredLevel = new AttributeRequiredLevelManagedProperty(AttributeRequiredLevel.None),
MaxLength = 100, // The MaxLength defined for the string attribute must be greater than the length of the AutoNumberFormat value, that is, it should be able to fit in the generated value.
DisplayName = new Label("Account Number", 1033),
Description = new Label("Account number for account.", 1033)
}
};
service.Execute(widgetSerialNumberAttributeRequest);
}
catch (Exception ex){
Console.WriteLine(ex.Message);
}
}
After running the above code one time, it will create an Auto Number field (new_accountnumber) in Account entity, you need to place this field (new_accountnumber) on CRM entity form and you might would like to also lock it from form customization.
After placing the field on the form publish the customization and create a record an Account record in CRM.
Other Auto Number Format Options are below
Auto Number Format value |
Example value |
ACC-{SEQNUM:3}-{RANDSTRING:4} |
ACC-123-AB7LSF |
CAN-{RANDSTRING:4}-{SEQNUM:4} |
CAN-WXYZ-1000 |
{SEQNUM:6}-#-{RANDSTRING:3} |
123456-#-R3V |
KA-{SEQNUM:4} |
KA-0001 |
{SEQNUM:10} |
1234567890 |
QUO-{SEQNUM:3}#{RANDSTRING:3}#{RANDSTRING:5} |
QUO-123#ABC#PQ2ST |
QUO-{SEQNUM:7}{RANDSTRING:5} |
QUO-0001000P9G3R |
CAS-{SEQNUM:6}-{RANDSTRING:6}-{DATETIMEUTC:yyyyMMddhhmmss} |
CAS-002000-S1P0H0-20170913091544 |
CAS-{SEQNUM:6}-{DATETIMEUTC:yyyyMMddhh}-{RANDSTRING:6} |
CAS-002002-2017091309-HTZOUR |
CAS-{SEQNUM:6}-{DATETIMEUTC:yyyyMM}-{RANDSTRING:6}-{DATETIMEUTC:hhmmss} |
CAS-002000-201709-Z8M2Z6-110901 |
Note:
- You can also convert an existing CRM text field to Auto Number field.
- You cannot create an auto number field in CRM with other special type field data types (for ex: Phone, URL ) . It should be a simple text field.
Reference MS Documentation: https://docs.microsoft.com/en-us/dynamics365/customerengagement/on-premises/developer/create-auto-number-attributes
*This post is locked for comments