Overview

In our projects, we have a requirement in which we need to have the functionality to generate some numbers (prefix or suffix) on the creation of a record. Most of the time, we end up undertaking a development or implementation of this through custom code, specifically through plug-ins.

Traditional Approach

In most of the projects, traditionally, we undertake the following approach to generate the auto-number functionality

Dynamics CRM 9.0 Onwards

We have certain Metadata properties of Attributes which have been introduced in CRM 9.0 onwards which are not visible through the User Interface but which we can set and play around with through a custom code approach, including Console Application.

To implement this approach, one needs to have a basic understanding of developing and implementing console applications. This helps us get the context of “Organization Service” from the CRM where we have certain requests available to undertake action over the properties of attribute, i.e., entities. In short, we can say this gives us the power to undertake action over everything in Dynamics CRM.

We can create an attribute in CRM programmatically by setting in certain properties of AttributeMetadata which is available in Microsoft.Xrm.Sdk.Metadata (Namespace)

Code

We need to create the attribute programmatically and once the same is created, we need to enable the auto-number logic that will be handled by the attribute on its own over the entity and record creation.

  1. Var attributeMetaData = new StringAttributeMetadata() {
  2. AutoNumberFormat = "Sum {RANDSTRING: 4} - Gup {SEQNUM: 4}",
  3. SchemaName = "sum_AutoNumAtt",
  4. RequiredLevel = new AttributeRequiredLevelManagedProperty(AttributeRequiredLevel.None),
  5. DisplayName = new Microsoft.Xrm.Sdk.Label("Entity Code", 1033),
  6. Description = new Microsoft.Xrm.Sdk.Label("The value will be AUTO GENERATED", 1033),
  7. IsAuditEnabled = new Microsoft.Xrm.Sdk.BooleanManagedProperty(false),
  8. IsGlobalFilterEnabled = new Microsoft.Xrm.Sdk.BooleanManagedProperty(true),
  9. MaxLength = 100
  10. };
  11. CreateAttributeRequest attRequest = new CreateAttributeRequest() {
  12. EntityName = entityName,
  13. Attribute = attributeMetaData
  14. };
  15. Service.Execute(attRequest); {
  16. EntityName = entityName,
  17. Attribute = attributeMetaData
  18. };

Observation/Benefits of implementing the same

Regarding End to End Implementation

There have been multiple challenges and multiple road-blocks faced while I tried undertaking this, so please feel free to get in touch if you feel stuck at any point.

Feel free to share your queries and, of course, the feedback.