
Originally Posted by
juniovalerio
In the process of structuring an appropriate platform and list of entries, I find myself in the necessities of assigning to each contract input in the database, a tag, more precisely a progressive code of number to be given automatically by the system when I record the project. (0001, 0002, 0003, 0004....000N).
It is not 100% resilient because it is not atomic but this can quite easily be achieved with a custom field and a small bit of custom logic.
Add a custom field project_number_c type integer.
Then create a before_save logic hook that does
PHP Code:
if (empty($bean->project_number_c))
{
$query "select max(project_number_c)+1 as newnum from project_cstm";
$res = $bean->db->query($query);
$row = $bean->db->fetchByAssoc($res);
$bean->project_number_c = $row['newnum'];
}
if you can risk (the small risk of) getting duplicates if two projects are saved in a very short time then this is your easiest solution. Otherwise you will have to declare the field project_number_c as auto increment in the database after sugar created it.
Bookmarks