Create Auto Number- Dynamics 365

Generally we will get a requirement to implement autonumber either for OOB entity or custom entity.  In previous versions of CRM we are creating custom entity to implement the scenario. But in dynamics 365 we can do this easily with oob or using CRM SDK.

In powerapps while creating the field  have data type as AutoNumber and can input the prefix, suffix. But in dynamics 365 we don’t have the front end UI. We need to create the field through backed code using CRM SDK.

In PowerAPPs UI.

AutoNumber Power App.PNG

This is one time process of creating autonumber from back-end code and can move to higher environments through solutions.

Create Autonum

public static void CreateAutoNumberAttribute()
{
string entityName = “new_righttoforget”; string filedschema = “new_rtfnumduplicate”;
Console.WriteLine(“Create Auto number Attribute for Entity {0}”, entityName);
CreateAttributeRequest req = new CreateAttributeRequest()
{
EntityName = entityName,
Attribute = new StringAttributeMetadata()
{
AutoNumberFormat = “RTF-{RANDSTRING:4}-{SEQNUM:4}”,
SchemaName = filedschema,
RequiredLevel = new AttributeRequiredLevelManagedProperty(AttributeRequiredLevel.None),
DisplayName = new Label(“RTF Number”, 1033), //Lable Name
Description = new Label(“The value will be AUTO GENERATED”, 1033),
IsGlobalFilterEnabled = new BooleanManagedProperty(true),
MaxLength = 100 //
}
};

service.Execute(req);
Console.WriteLine(“Created Auto number Attribute for Entity {0}”, entityName);

}

Autonumber Format Options

AutoNumberFormat value Example value
CAR-{SEQNUM:3}-{RANDSTRING:6} CAR-123-AB7LSF
CNR-{RANDSTRING:4}-{SEQNUM:4} CNR-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

Auto Number

That’s it. Enjoy 🙂

One thought on “Create Auto Number- Dynamics 365

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s