Auto numbers or unique alphanumeric strings are usually needed by CRM users to uniquely identify records in UI. We have developed plugins in the past, which would generate unique identifiers for us by using counters or random strings.

Microsoft has released an “Auto number” attribute in the 9.0 version of Dynamics 365. Adding the attribute from UI is not allowed yet, but it will be soon. Until then, we can add the attribute via API.

Let’s add one.

1. Create a console application in Visual Studio.

new auto number

2. Add the Dynamics 365 V9.0 dlls as references. Currently, the preview version of the dlls is available in NuGet.

https://www.nuget.org/packages/Microsoft.CrmSdk.CoreAssemblies/9.0.0.4-Preview

3. Create a connection to your organization and call the CreateAutoNumberAttribute function from the Main() function.

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using System.Configuration;

using Microsoft.Xrm.Sdk;

using Microsoft.Xrm.Sdk.Query;

using Microsoft.Xrm.Sdk.Client;

using Microsoft.Xrm.Sdk.Messages;

using Microsoft.Xrm.Sdk.Metadata;

using System.ServiceModel.Description;

static
void Main(string[] args)

{


string orgServiceUrl = ConfigurationManager.AppSettings[“orgServiceUrl”];


string userName = ConfigurationManager.AppSettings[“username”];


string password = ConfigurationManager.AppSettings[“password”];


Uri oUri = new
Uri(orgServiceUrl);


//** Your client credentials


ClientCredentials clientCredentials = new
ClientCredentials();

clientCredentials.UserName.UserName = userName;

clientCredentials.UserName.Password = password;


//Create your Organization Service Proxy


OrganizationServiceProxy _serviceProxy = new
OrganizationServiceProxy(

oUri,


null,

clientCredentials,


null);

CreateAutoNumberAttribute(“account”, “new_accountnumber”, _serviceProxy);

}

4. Following function creates an auto number attribute of “string” type in “Account” entity.

“AutoNumberFormat” is the new property that has been added to the AttributeMetadata class to help us add the auto number attribute.

public
static
void CreateAutoNumberAttribute(string entityName, string attributename, OrganizationServiceProxy _orgServiceProxy)

{


CreateAttributeRequest newAttributeReq = new
CreateAttributeRequest

{

EntityName = entityName.ToLower(),

Attribute = new
StringAttributeMetadata

{

AutoNumberFormat = “ACC-{RANDSTRING:6}”,

LogicalName = attributename.ToLower(),

SchemaName = attributename.ToLower(),

MaxLength = 150,

RequiredLevel = new
AttributeRequiredLevelManagedProperty(AttributeRequiredLevel.None),

FormatName = StringFormatName.Text,

DisplayName = new Microsoft.Xrm.Sdk.Label(“Account Number”, 1033),

Description = new Microsoft.Xrm.Sdk.Label(“Unique alphanumberic number for each account”, 1033)

}

};

_orgServiceProxy.Execute(newAttributeReq);

}

5. Time to check out the new attribute!

new auto number

6. Let’s create few accounts and check the auto numbers!

new auto number

7. 9.0 version of Dynamics 365 supports the following tokens for Auto numbers:

Static String
DATE:[format]
SEQNUM:size
RANDSTRING:6

There you have it! We’re happy to have this feature added to Dynamics 365. Be sure to check out our blog for more updates, news, and tips for Dynamics 365!

Happy Dynamics 365’ing!