This is a trick one of my co-workers (Amreek Singh) showed me a little while ago. This will work in CRM 4 and CRM 2011, although, for convenience, I’m doing it in the 2011 beta.
Current Auto-Numbering Options
The first option is for a limited set of entities which have auto-numbering out of the box.
For Contracts, Cases, Articles, Quotes, Orders, Invoices and Campaigns, auto-numbering can be set in the Administrative settings in CRM. The prefix for each of the entity numbers can be set and the final suffix can be of a length from four characters to six characters. A word of warning, if you change the length of the prefix, this changes for ALL the entities, not just the one you’re on.
For other entities, if you are using CRM 4, there is also an auto-numbering plugin available from codeplex.
Of course, if you are using the online version of CRM 4 or CRM 2011, you will need to resort to another option. Here is one using workflow.
Step One: An Entity to Store the Current Incrementing Value
To store the current value of the incrementing value, we need a new entity. For this entity we create one record to hold the values.
In this case I’m storing the prefix and the suffix I am going to use for an incrementing value on the account record.
The record also needs to have 1:N relationship to the records it will increment. In this case, the Account entity.
Step Two: Set up the Field on the ‘Numbered’ Record
Account already has an ‘Account Number’ field so I will use this field to hold the value of the field.
Step Three: Set up the Workflows
I tried doing this with one workflow but for some reason the workflow could not link to the record with the values in it and then populate them. Therefore, I’m using two. The first workflow runs on the creation of the Account record and links to the record storing the increment values.
Incidentally, there is no need to add the lookup to the Account form as workflows can populate fields whether or not they have been added to the form. This is great when you need to store values but do not want to clutter the forms up.
The second one is triggered when the link to the value store record is populated. It does the populating of the Account record and also increments the value store by one so the next Account record gets a different suffix value.
For the Account Number, we add the two ‘slugs’ divided by a dash.
For the incrementing of the storing record, I use a little known trick which is available in both versions of CRM.
When you use the Form Assistant to populate a field, drop the ‘Set To’ Operator down and you will see a whole range of different operators available, depending on the field type. In the case of integer fields, one of the options is ‘Increment By’. By adding ‘1’ to the Default Value and hitting OK we tell the workflow to increment by 1 every time the workflow fires.
What We End Up With
After all this we end up with a process which will populate the Account Number of an Account with a unique value every time an Account record is created. As usual with workflows, this runs asynchronously. In other words, you will NOT see the field populating on the initial creation but rather only after you have saved and closed the record and reopened it a little time later, after the workflows have finished.
We can extend this to multiple entities by adding other fields to the store record and setting up additional 1:N relationships from the store record entity.