What I have here is a simple logic hook for adding auto sequencing codes to your custom field.
There seems to be a lot of threads on this so I thought I'd share this generic generator that can be used in a variety of circumstances.
It creates codes in the manner, PREFIX DATE_FORMAT SEPARATOR AUTO_NUMBER SUFFIX
eg, in the attached image I've used the following LC 08 - 0001
All the options are configurable in the hook in the constants section.
I've tested and using it in Leads, Opportunities, Projects, and many custom modules.
CODE_PREFIX = "LC"; //prefix for the code
CODE_SUFFIX = ""; //suffix to add after the code
CODE_SEPARATOR = "-"; //separator to use
CODE_FIELD = "lead_code_c"; //custom field to be generated
CUSTOM_TABLE = "leads_cstm"; //table containing custom fields
ADD_DATE = "1"; //add the date
DATE_FORMAT = "y"; //format for the year parameter
ZERO_PADDING = 4; //minimum amount of characters desired for the number. ie 4 = 0001, 3 = 001
FIRST_NUM = "0001"; //default to start with.
It is designed to be used as an after_save hook and is compatible with create records from a subpanel as it doesn't rely on the custom table entry already existing.
Add a custom field to your module in studio, noting the name of the field. In my example I've use leads module and added the field lead_code which is called lead_code_c in the database.
Extract and copy add_code_hook.php to custom/modules/<module>/
In logic_hooks.php add the following
Configure your options in add_code_hook.php
$hook_array['after_save'] = Array();
$hook_array['after_save'] = Array(1, 'add_code', 'custom/modules/<module>/add_code_hook.php','add_code', 'add_code');
There is probably a couple of cases where this won't work as expected but for most generic needs it's quite adaptable.
Let me know if you have any issues or need a custom code that's requires a special regex match.