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.
  • The comma separation on decimal numbers must be turned off (to an empty space) and the spacing moved so that there is none. This can be achieved in personal and global settings via settings > admin > system settings > formats. (Note: I did read up on a workaround with using JavaScript instead of this, but not sure if it would work, so any feedback where this has been would be appreciated).
  • 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.

Step 1:

  • Create two entities: ‘Auto Number Definition’ & ‘Auto Number Request’
  • Make them both available via ‘Settings’ and uncheck all options for entity. Ownership is ‘Organisation’.
IncrementalNumbering_1

Two new entities – Auto Number Definition & Auto Number Request

IncrementalNumbering_2

Untick everything except make them available in ‘Settings’ and Ownership is ‘Organisation’

Step 2:

Fields for Auto Number Definition are below. Note: Repeat items 2 & 3 for the number of ‘Types’ i.e. Incremental Numbers you require.

  1. Increment By:  Decimal Number
  2. 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’
  3. Last Number (Prefix Field One): Decimal Number e.g. ‘Last Number(EASY)’ & Last Number(HARD)’
IncrementalNumbering_3

Fields for Auto Number Definition

Step 3:

Fields for Auto Number Request are below:

  1. Name
  2. Auto Number Definition (Lookup)
  3. Prefix Req: Global Option set used on your custom entity
  4. New Number: Decimal
  5. Prefix: Single line of text
IncrementalNumbering_4

Fields for Auto Number Request

Step 4:

  • Arrange your fields on your forms.

Step 5:

  • 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.
IncrementalNumbering_5

The single Auto Number Definition Record

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:

Step 6:

IncrementalNumbering_6

Initial Setup of the Contact Auto Number workflow

  • 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.

Step 7:

  • 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:

IncrementalNumbering7

First Workflow Logic

Creating the Auto Number Request – setting the initial fields before the second workflow picks it up

IncrementalNumbering9

When the second workflow has done it’s thing – you pick it back up from the local values list – this is the one highlighted. If you are repeating these steps due to the number of ‘Types’ always select the bottom in the list as you move from top to bottom i.e. group 1, group 2.

IncrementalNumbering10

Once you have selected the local value, select the prefix from the Auto Number Request Record

IncrementalNumbering12

Select ‘Append With’ on the second update, as the Prefix already resides in this field, we want to now update the Unique ID with the Last Number that has just been incremented

 

IncrementalNumbering13

Appending the Unique ID with the Last Number so the result is Prefix + Last Number

 

Step 8:

  • 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

  1. 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.
  2. 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.
  3. Stop Workflow with a Status of Succeeded
IncrementalNumbering14

Setup of the Auto Number Request – Contact Workflow

IncrementalNumbering15

Auto Number Request – Logic Setup

IncrementalNumbering16

Navigate to the right of the screen and click on the drop down operator to use ‘Increment By’. You need to Increment the Last Number (Type X) that is related to the type selected by the user.

IncrementalNumbering18

IncrementalNumbering19

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.

IncrementalNumbering