This post is going to detail a walk through on how to create an Incremental ID Solution for an entity -either an oob or a custom entity – both work fine. You also can take and improve/use only part of the solution. The inspiration I used for this and what it builds upon is the guide I found here. What I did beyond this guide was add the Prefix element and work it for more than one type i.e. prefix so all of the prefixes have separate incremental numbers. So type 1 would be ABC -00001 but type 2 would also be XYZ-00001, and so on. I have this successfully working on 9 types currently but is fully scalable up or down.
Before you start…
The following list includes certain notes that must be true if you was to use this solution. They could be deal-breakers if your client is not happy with some of them, so I’d rather them be listed at the start so you know if they will be useful or not.
- The Incremental Number element of the ID must start with 1. e.g. 10000.
- If you are using a ‘Type’ i.e. a Project (custom entity) is of a certain ‘Type’ then the prefix is XYZ, if it is of Type 2 then the prefix is ABC, it is advised you use a global option set to identify these types inside your custom entity as the same ones can then be used in your incremental numbering solution.
Now to begin…
I’ll screenshot the way forward to make this easier. I recommend you follow best practices and use a new solution for this and bring in any existing entities or option sets you are intending to use. I have brought in the Contact entity, customised it to include a field called ‘Unique ID:’ and ‘Type’ which are both custom, one with a global option set of two types – this is going to determine my prefix in my Unique ID.
- Create two entities: ‘Auto Number Definition’ & ‘Auto Number Request’
- Make them both available via ‘Settings’ and uncheck all options for entity. Ownership is ‘Organisation’.
Fields for Auto Number Definition are below. Note: Repeat items 2 & 3 for the number of ‘Types’ i.e. Incremental Numbers you require.
- Increment By: Decimal Number
- Prefix Field One: Single line of text (what your prefix will be for Type 1) e.g. ‘Easy Project’ & ‘Hard Project’ – in the screenshot below this is ‘Type 1’
- Last Number (Prefix Field One): Decimal Number e.g. ‘Last Number(EASY)’ & Last Number(HARD)’
Fields for Auto Number Request are below:
- Auto Number Definition (Lookup)
- Prefix Req: Global Option set used on your custom entity
- New Number: Decimal
- Prefix: Single line of text
- Arrange your fields on your forms.
- Create 1 Auto Number Definition Record and populate the fields based on your requirement. This could be to increment by 1, 2 or 10 and do the same for your prefix.
When your Incremental Number begins
The original guide created their Incremental Numbering on the creation of a Contact record, however mine creates it on the Update of the ‘Type’ option set field in the Contact entity. It is important to remember it can be whatever you want it to be you just need to write it into your workflow, but also remember to put in checks in place to ensure your Incremental Numbering doesn’t overwrite itself. To see how this is done, see below:
- Initial Setup of the Workflow ‘Contact Auto Number’ – Create a workflow called ‘Contact Auto Number’ (or whatever entity yours is on). It is a Workflow (Process). Convert it to a Real Time Workflow. Scope is Organisation. Mine is set to start when was AFTER record fields change, but yours can be anytime you need it to be, you just need to configure as necessary.
- Now to implement your business logic. If yours is configured to run when records are changed i.e. update, it is best to put some checks in place to make sure your ID is not overwritten in case the fields are updated a second, third, fourth etc time, as you’ll end up with four different, new, ID’s, which we don’t want to happen.
The first step in your workflow needs to be a condition such as:
if X field (where X is the field your ID is on your entity i.e. Unique ID) does not contain data:
Then depending on the number of ‘types’ i.e. prefixes you have, these are essentially going to be their ‘Groups’. For this demonstration I have two types, so i’ll need to repeat the following steps below twice. I have implemented this solution for 9 types before and so I needed to have 9 stages. It does get repetitive so try not to fall into the relaxed slumber of copy and paste.
Condition: If the Contact Type is ‘Type 1’ .. (replace ‘Type 1’ for ‘Type 2’ and so in for the proceeding stages)
- Create a new Auto Number Request record: Name = any arbitrary name, I picked ‘Auto Number Request for Contact’. Set the Auto Number Definition Lookup to the lookup to your single record you made earlier, Prefix Req. = Type 1 (this will be changed to Type 2′ etc for your proceeding stages (From your global option set on your Entity). <What this would have now done, is kicked off your second workflow which is described above, done it’s business, and then in the same transaction, picked up the following steps to updating your Entity (In my case Contact) with the new fields) from this workflow.
- Update Contact – the first one, click on your Unique Number and click ‘Set To’ then scroll down to the bottom under ‘Local Process’ and you will see ‘Create – Auto Number Request’ (This may differ depending on the title of your workflow) select this and then pick ‘Prefix’. Its important to remember if your repeating this steps you will have multiple different local process values with the same name as above, you need to move down each step e.g. to the second one for your second condition, the third for your third etc.
- Update Contact – the second one, click on your Unique Number and click ‘Append To’ and scroll down to the bottom and you will see the same ‘Create – Auto Number Request’ select this and pick ‘New Number’.
- Stop workflow with a status of succeeded.
Repeat those steps for each different option set/prefix you want to set. Mine is two, so i have two sets of this sequence:
- Now create a second workflow, all same configuration as above except this is a bit more rigid. It needs to be for the Auto Number Request entity, a Real Time workflow and on the Create of a Auto Number Request.
The idea here is you need to make the number of clauses you did in the first one in this one too – so i have two types, I did two clauses, one for each type, in my first workflow, so I’ll need to do the same here to accommodate for them both. This can be expanded on for how ever many types you have – yes it can get repetitive, but for the sheer joy of doing this all in a workflow than in a plugin should make up for it 😉
If Auto Number Request ‘Prefix Req’ <equals> ‘Type 1’ then.. etc
- Update Auto Number Definition: You are using the Operator: ‘Increment By’ and this will be on the field ‘Last Number(Type 1) to be ‘Increment By(Auto Number Definition(Auto Number Definition) – your basically using the number in your Increment By to increment the Last Number in this field by that amount.
- Update Auto Number Request: Update your ‘New Number’ field with the Last Number(Type 1) from the Auto Number Definition Record you just incremented and the Prefix is the Prefix on the Auto Number Definition Record for your type, in my case it is held in the ‘Type 1’ field so pull this too. It is important to remember depending on the amount of prefix clauses your going to enter your going to have that number created in this workflow instance, so always work top to bottom and the one you want will always be at the bottom.
- Stop Workflow with a Status of Succeeded
There you have it, a working incremental numbering solution using only real time workflows. This solution is only compatible for CRM 2013 and above as this is when the real time workflows got introduced and is unfortunately not a feature of CRM 2011 and earlier. If you are trying to implement and experience any issues, please do leave a comment and I’ll do my best to help you.