Home > CRM, Dynamics 365 > Dynamics CE – Demystify ‘Auto Number’

Dynamics CE – Demystify ‘Auto Number’

Auto Number‘ feature is available with Dynamics 365 (online) and V9.x versions. With this feature we are no longer dependent on 3rd party ISV solutions for Auto numbering.

Auto Number - 2

In this article I am going to cover a few use cases of Auto Number fields.

How to create an Auto Number field:

As of the date, we can either create a new ‘Auto number’ field or convert an existing field to ‘Auto Number’ only programmatically (i.e., using SDK call).

Creation of an ‘Auto Number’ from Application will be included in next releases.

Lets see how to create a brand new ‘Auto Number’ field and convert an existing ‘Single Line Of Text’ field to ‘Auto Number’ along with few key points.

[Code Snippet] Create a NEW auto number field:

Below is the sample code to create a new Auto Number field ‘new_studentnumber‘ in a custom entity ‘new_student

var studentNumberAttributeRequest = new CreateAttributeRequest{
EntityName = “new_student“,
Attribute = new StringAttributeMetadata{
//Define the format of the attribute
AutoNumberFormat = “STD-{SEQNUM:4}”,
// Set fields Logical and Schema Name
LogicalName = “new_studentnumber“,
SchemaName = “new_studentnumber“,
RequiredLevel = new AttributeRequiredLevelManagedProperty(AttributeRequiredLevel.None),
MaxLength = 100,
// Set fields Display Name and Description
DisplayName = new Label(“Student ID”, 1033),
Description = new Label(“Student ID”, 1033)
}
};
_serviceProxy.Execute(studentNumberAttributeRequest);

  • Make sure you ‘Publish‘ customization’s from Application after executing the code.

[Code Snippet] Convert EXISTING field to an auto number field:

Below is the sample code to convert OOB ‘new_name’ field to auto number field in the custom entity ‘new_student‘.

Important note:

  • Field you are converting has to be of type ‘Single Line Of Text’.
  • If you pass a different type of field (Ex- Whole number), code will not throw any exception but no auto number logic will get attached to the field.

// First retrieve the Attribute Metadata
var attributeRequest = new RetrieveAttributeRequest{
EntityLogicalName = “new_student”,
// Pass the attribute name
LogicalName = “new_name”,
RetrieveAsIfPublished = true
};

// Retrieve attribute response
var attributeResponse = (RetrieveAttributeResponse)_serviceProxy.Execute(attributeRequest);

// Now modify the retrieved auto-number attribute
var retrievedAttributeMetadata = attributeResponse.AttributeMetadata;
// Set the auto numbering format
retrievedAttributeMetadata.AutoNumberFormat = “STD-{SEQNUM:3}-{RANDSTRING:6}”;

// Update the auto-number attribute
var updateRequest = new UpdateAttributeRequest{
Attribute = retrievedAttributeMetadata,
EntityName = “new_student”,
};

// Execute the request
_serviceProxy.Execute(updateRequest);

  • Make sure you ‘Publish‘ customization’s from Application after executing the code.

Key Points:

  • Auto number fields will be Read-only and you cannot override the value from Form.
  • But you can override the value from code using SDK Insert call.
  • As you would notice below, Even though the ‘Name’ is an Auto number field, I could create a ‘Student’ record from code by setting ‘Name’ to ‘Rajeev’.  If you dont set a ‘Name’ field value from code, system would populate the auto number.

Auto Number - 1

  • In a single Entity, you can have multiple auto number type fields with the same format.
  • You can alter the auto number format of a field at any point of time by using ‘UpdateAttributeRequest’ SDK call (Refer 2nd code snippet above).
  • In the Auto number format, use ‘Random String’ tag (i.e., {RANDSTRING:number}) which help you to avoid duplicates or collisions, especially when offline clients trying to create auto-numbers.
    • For example, CNR-{RANDSTRING:4} format will generate an auto number with a random string of 4 character length (i.e., ‘CNR-WXYZ‘)

Auto Number - 3

  • The random string placeholders are optional.You can include more than one random string placeholder in an Auto Number Format.

🙂

 

Advertisement
  1. Chirag
    June 20, 2018 at 2:58 PM

    How can I remove auto number if that is no longer required. Eventually, it should not generate any number sequence and field should be unlocked on form?

    Thanks
    Chirag

    • venkat
      January 15, 2019 at 3:55 PM

      I have the same question as Chirag had .
      We have updated an existing field as autonumber , but we want to revert it back to normal field because having an auto number field is increasing the save time of the record atleast 20-30 seconds .
      I have updated the attribute by setting the auto number value to null , but it seems not to work . I dont get auto number now , but the record saving time is still more . Used the below code for setting auto number to null :

      var retrievedAttributeMetadata = attributeResponse.AttributeMetadata;
      // Set the auto numbering format
      retrievedAttributeMetadata.AutoNumberFormat = null;

  1. March 11, 2019 at 1:21 AM

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 )

Facebook photo

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

Connecting to %s

%d bloggers like this: