Sample Code for Autonumbering in Dynamics 365 July 2017 Update


As a first step, add references to the latest version 9 assemblies

https://www.nuget.org/packages/Microsoft.CrmSdk.CoreAssemblies/

In the below sample code, we are creating an Auto number attribute in Contact Entity.

This is how the fields shows up inside contact form.

Using UpdateAttribute Request we can update the format for the AutoNumber field.

And also, we can have multiple auto number fields for an entity as shown below.

Sample Code


CreateAttributeRequest createAttributeRequest = new CreateAttributeRequest();
createAttributeRequest.EntityName = "contact";

var autoNumberAttributeMetadata = new StringAttributeMetadata()
{
AutoNumberFormat = "Auto Number - {SEQNUM:4} - {RANDSTRING:4} - {DATETIMEUTC:yyyyMMddhhmmss}",
SchemaName = "new_autonumber1",
MaxLength = 100,
RequiredLevel = new AttributeRequiredLevelManagedProperty(AttributeRequiredLevel.ApplicationRequired),
DisplayName = new Microsoft.Xrm.Sdk.Label("My Auto Number", 1033),
Description = new Microsoft.Xrm.Sdk.Label("This is my first auto number field through SDK", 1033)
};

createAttributeRequest.Attribute = autoNumberAttributeMetadata;
var response = organizationService.Execute(createAttributeRequest);

And now the simplest and best way to create these Auto Number fields à

Using Auto Number Manager developed by Jonas Rapp, Microsoft MVP.

https://www.linkedin.com/pulse/auto-number-attributes-microsoft-dynamics-365-jonas-rapp/

https://www.xrmtoolbox.com/

https://github.com/MscrmTools/XrmToolBox

Hope it helps..

Advertisements

Author: Nishant Rana

I love working in and sharing everything about Microsoft.NET technology !

3 thoughts on “Sample Code for Autonumbering in Dynamics 365 July 2017 Update”

  1. Hello,
    Just wondering if using xrmtoolbox to generate the number, what do I need to do to include it in the solution so I can import it to another environment? Thanks.

    Like

    1. Nothing Mark. It makes this change at the meta data level. It is just like creating a new field, so when you are moving the entity/field it will move along with it as part of solution.

      Like

  2. We are currently based in Pakistan and our D365 online servers are deployed in Europe. we need to create auto number with datetime sequence added. Hoever only option available is DATETIMEUTC. But UTC always create date & time with 5 hours difference, hence resulting wrong sequence. If there is any way to overcome this issue.

    Like

Leave a Reply to Mark Cancel 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 )

Google photo

You are commenting using your Google 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

This site uses Akismet to reduce spam. Learn how your comment data is processed.