303 words
2 minutes
Creating and Managing AutoNumber Fields in Dynamics 365 CE Using C# SDK

Creating and Managing AutoNumber Fields in Dynamics 365 CE Using C# SDK#

AutoNumber fields in Dynamics 365 automatically generate unique identifiers for records, helping to streamline data management, improve record tracking, and enhance user experience. Let’s walk through the steps to create, update, and format AutoNumber fields using the Dynamics 365 C# SDK.

Creating an AutoNumber Field#

1. Initialize Your Dynamics 365 Service#

First establish a connection to your Dynamics 365 organization:

// Initialize IOrganizationService or IOrganizationServiceFactory
IOrganizationService _service = GetOrganizationService();

2. Define the AutoNumber Field#

StringAttributeMetadata autoNumberField = new StringAttributeMetadata
{
    LogicalName = "new_autonumberfield",
    DisplayName = new Label("AutoNumber Field", 1033),
    RequiredLevel = new AttributeRequiredLevelManagedProperty(AttributeRequiredLevel.None),
    MaxLength = 100,
    AutoNumberFormat = "AUTO-{SEQNUM:4}-{RANDSTRING:4}"
};

3. Create the AutoNumber Field#

CreateAttributeRequest createAutoNumberRequest = new CreateAttributeRequest
{
    EntityName = "account", // Example: account entity
    Attribute = autoNumberField
};

_service.Execute(createAutoNumberRequest);

Updating an Existing Field to AutoNumber#

1. Retrieve the Existing Attribute#

RetrieveAttributeRequest retrieveRequest = new RetrieveAttributeRequest
{
    EntityLogicalName = "account",
    LogicalName = "existing_field",
    RetrieveAsIfPublished = true
};

RetrieveAttributeResponse retrieveResponse = (RetrieveAttributeResponse)_service.Execute(retrieveRequest);

2. Update the Attribute as AutoNumber#

StringAttributeMetadata existingField = (StringAttributeMetadata)retrieveResponse.AttributeMetadata;
existingField.AttributeType = AttributeTypeCode.String;
existingField.AutoNumberFormat = "NEW-AUTO-{SEQNUM:3}-{RANDSTRING:2}";

UpdateAttributeRequest updateRequest = new UpdateAttributeRequest
{
    Attribute = existingField
};

_service.Execute(updateRequest);

Changing AutoNumber Field Format#

Follow the same steps as updating an existing field, but only modify the AutoNumberFormat property:

existingField.AutoNumberFormat = "UPD-{SEQNUM:5}-{DATETIMEUTC:yyyyMMdd}";

Setting the Seed Value#

SetAutoNumberSeedRequest setSeedRequest = new SetAutoNumberSeedRequest
{
    EntityName = "account",
    AttributeName = "new_autonumberfield",
    Value = 5000 // Custom starting value
};

_service.Execute(setSeedRequest);

AutoNumber Format Options#

TokenDescriptionExample Output
{SEQNUM}Sequential number1, 2, 3…
{SEQNUM:3}Sequential number with padding001, 002…
{RANDSTRING}Random alphanumeric charactersA1B2
{RANDSTRING:4}Random string with specified lengthX9Y8
{DATETIMEUTC}Current UTC date/time20230515
{DATETIMEUTC:format}Custom datetime format2023-05-15
Creating and Managing AutoNumber Fields in Dynamics 365 CE Using C# SDK
https://crmte.ch/posts/autonumbersdk/
Author
Anu Prakash
Published at
2023-10-08
License
CC BY-NC-SA 4.0