Auto Numbering Attributes Creation - Dynamics 365, V 9.0 (Online)

Auto Number Creating steps


Prior to this V9 release, we used to generate Auto Numbers with the help of counters and random strings for an entity by writing plugins.

With this release now we can create Auto Number attribute to any entity in CRM. Currently, we can do that programmatically as adding it from UI is not allowed yet.

To create auto number attribute, create a console app and connect to desired CRM instance and use the below code snippet in that.

Here, new_accunum is the new field which we are creating from CONSOLE APP as an Auto Number Attribute. Once the attribute is created in CRM with the given sequence, whenever a new record created for the respective entity automatically these fields get updated. Below is a screenshot for reference.


CreateAttributeRequest CreateANbrAttribute= new CreateAttributeRequest

{

    EntityName = "account",

    Attribute = new StringAttributeMetadata

    {

        //Define the format of the attribute
        AutoNumberFormat = "ACC-{SEQNUM:4}-{RANDSTRING:6}",
        LogicalName = "new_accunum",
        SchemaName = "new_accunum",
        RequiredLevel = new AttributeRequiredLevelManagedProperty(AttributeRequiredLevel.None),
        MaxLength = 100,
        DisplayName = new Label("Account Serial Number", 1033),
        Description = new Label("Alphanumber for each Account record, which is unique..", 1033)
    }
};
_serviceProxy.Execute(CreateANbrAttribute);

Sample Auto Number


Please refer this for different Auto Number options.

In the same, we can use an existing field (single line of text) for Auto-Numbering. As we are trying to re-use an exiting field, first we need to retrieve it and set the Auto Numbering format to that attribute's metadata

Let's see how it works... In this example, you can find the complete method created to update an existing field with an auto-numbering format in CRM.


 public static void AutoNumbering(IOrganizationService _service)
        {
            try
            {             
                RetrieveAttributeRequest attributeRequest = new RetrieveAttributeRequest
                {
                    EntityLogicalName = "cr133_permission",
                    LogicalName = "cr133_name"
                };
                RetrieveAttributeResponse attributeResponse = (RetrieveAttributeResponse)_service.Execute(attributeRequest);
                AttributeMetadata retrievedAttributeMetadata = attributeResponse.AttributeMetadata;
                retrievedAttributeMetadata.AutoNumberFormat = "{SEQNUM:10}";

                UpdateAttributeRequest updateANbrAttribute = new UpdateAttributeRequest
                {
                    Attribute = retrievedAttributeMetadata,
                    EntityName = "cr133_permission",             
                };
                _service.Execute(updateANbrAttribute);             
            }
            catch (Exception ex)
            {
                Console.WriteLine("Exception Message: " + ex.Message);
                Console.ReadLine();
            }
        }


No comments:

Post a Comment