Thursday, January 15, 2009

Creating custom Item ID's

A customer would like to use custom ID's to track items throughout the site. These ID's are something like:
TYPE_DATE_ID

I would like to delete the Title field and use this throughout the system, but this causes some problems. I can't rename Title without renaming Title on every item in the entire site. I can hide title, and create my own column, but then I have to create custom datasheet views everywhere because Title is the only field that has a link to the new form, and has the drop down list available. I suppose I'll just have to use the Edit link everywhere.

Creating a custom ID field should be easy right? Just use a calculated field you say!
Sadly, it's not that simple. First, I would like to have the ID portion padded to 4 spaces. ie. 0001, 0021, etc. This causes some troubles. Second and most important, the ID field is set after the calculated field is set. If you do this strictly through a calculated field, you'll end up with TYPE_DATE_0000.

Ok, so then do it with a workflow! This solves the ID being 0 problem, but the Designer workflows don't seem to support the formula's needed to create the Date in a YYYYMMDD format. If you just use Created as the date, you'll end up with something like: TYPE_1/15/2009 8:43:03 AM_0001. UGLY.

The solution is to use a hybrid of both. Create a calculated field that is:
=TEXT(Created,"yyyyMMdd")
Next, create a workflow that is activated on New item created for you're list. The workflow will need two steps.
The first step is to create the necessary padding for your ID. Mine is something like:

If ID < 10 set variable 'padding' = '000'
if ID >=10 and ID <100 set variable 'padding' = '00'
If ID >=100 and ID <1000 set variable 'padding' = '0'

Next, set the prefix = 'TYPE' or whatever you want. If this is dynamic, you'll need another step to set this to be what you want.

Use the string builder to put all your variables together. [PREFIX]_[createDate]_[padding][ID]

Set your Identifier field to the new variable you created with the string builder. DONE!

No comments: