A common request for Auto Numbers in Microsoft Dynamics 365 (CDS/CRM) is to have one number field with different formats and unique sequences based on data on the record or it’s parents.
I call this “Conditional Unique Auto Number Sequences”.
In a previous article I demonstrated one way of including dynamic information in a numbered attribute using a calculated field for the number. In this one I will take another approach, and also add the unique sequences.
On my Rocket entity I have an OptionSet defining the Purpose of the rocket.
The customer required (I know what you all think but my customer is not Elon, he made me promise to always deny that) a separate sequence for each Purpose, but they should all be shown in the same field.
As we know, this is not possible out of the box, so we have to bend the laws of nature a bit and come up with a little customization trick to satisfy him the customer.
Step by step
Generic Numbering Entity
First create a “generic numbering entity” that can be used for all numbering purposes in the system, where we need to up the game from OOB functionality.
Add fields for all different numbering sequences you need. In this case I have one sequence for Contacts, and four different for my Rockets, where the number sequence and format will depend on the chosen Purpose of the rocket.
The numbering fields can be configured something like below.
Click the image to see full configuration.
Real-time workflow
Now create a real-time workflow triggering on create of the entity to be numbered, Rocket in my case.
Selecting sequence to use
Check the Purpose field of the created rocket, and for each of the available options, create a branch in the workflow. In each branch, add a step to create a new record of the Cool Numbers entity.
Generating the unique number from the unique sequence
Set the name of the new record to indicate the source record (the new rocket) that triggered the creation of the numbering record. Then add a dash (-) to each of the auto number fields that will not be used for this condition. Leave the field for the selected number empty.
Adding the dashes will prevent these fields from being numbered, and thus “preserves” the numbers in their respective sequences.
Setting the number
Now add a step to update the rocket record that triggered the workflow. Set the Rocket Number field to the selected numbering field on the created numbering record.
Repeat this for all possible purposes.
Complete workflow
The complete workflow will now look like this.
Result
Creating new rockets in my environment, and they are now numbered according to my new unique sequences based on the selected purpose of each rocket.
Method in short
To create conditional unique auto number sequences for one single field, follow these simple steps>
- On entities to be numbered, create a normal text field to hold the number.
- Create a “generic numbering entity” (GNE) that can be used for all numbering purposes.
- For each unique sequence, create a field of type Autonumber and define numbering format.
- Create a real-time workflow for each entity to be numbered, triggering after create.
- Let the workflow check conditions defining which number should be used.
- Let the workflow create a new record of the GNE and set the value of all numbering fields except the one we want to use to “-“. Also set the name of the GNE record to the name of the record triggering the workflow.
- Let the workflow copy the value of the numbering field used on the GNE record to the numbering field on the triggering record.
Numbering existing records
Now that you have implemented this type of smart conditional numbering, you might be wondering what you should do with all existing records?
There is a solution to that as well. Just go back to a previous article in this series – Auto Numbering existing records in Microsoft Dynamics 365 / CRM.
What about Mirko?
Well this article didn’t really solve Mirko’s request on Facebook. But stay tuned for next article, I will show you how to use this technique to only assign numbers when specific conditions are met.
Hej Jonas,
thanks for the really good article. I think it is a nice solution to the problem.
One thing worth mentioning (at least I think so) about the solution:
If you add additional numbering fields to the generic numbering entity it is important to go back to all your workflows and add a dash (-) in the new field. Otherwise, it will increment the new field as well.
//Benedikt
Indeed Benedikt, good catch!
Hello Jonas,
Did you highlight grid cells using graphics editor, or it’s part of your solution? 🙂
Sorry Artem, those are just from my Snagit skills… ????
Hi Jonas,
Great article! Was wondering if you could share the link where you explain how to use this technique to only assign numbers when specific conditions are met? Thanks!
Easiest is to have a plugin or workflow or js or form rule to set the value to “null” pre save the record, then the auto numbering will not set a value.
This is a great one, but is there the same idea in dynamics 365 FO? I mean creating one field number sequence that is depending on the values of another fields?
Sorry, I know nothing about FnO 🙄