auto increment integer field

Hi,

I use sugar 7.6 pro and I want a custom integer field id_autoincrement in the task module which is autoincremented every time that I create a new task.

If I create a task and the last created id is 10 the new one will be 11 for example.

I find a lot of posts which explain how to do this but the most completed and good are the posts with the logic hooks solution.

How do you auto increment an "ID" field for every opportunity record? 

Auto Increment Integer Field in SugarCRM 7.6 On-Demand (SaaS) 

autoincrement field 

...and many others.

These solutions code do:

  1. get the max current id in the db
  2. increment the max id value
  3. assign the incremented value to the new record id

The problem is: if two new records get at the same time the max current id (e.g. max_id=10)  in the db, the two new created records will have the same id (e.g. new_id1=11  and  new_id2=11).

How can I avoid this problem?

Parents
  • Hi Stefano

                       Extending fields vardefs 'auto_increment'=>'true' would be the best approach.

    Regards

    Sidhu

  • I try with this but doesn't work:

    In this path custom\modules\Tasks\Ext\Vardefs\vardefs.ext.php I add this row 

    $dictionary['Task']['fields']['id_autoincrementale_c']['auto_increment']=true;

    But when I try to create a new record in the Tasks module the fields still remain empty or with the default value 1. If I try to change the default value from studio and add the start incrementing value, this erro is shown:

    Cannot modify field Query Failed: ALTER TABLE tasks_cstm modify COLUMN id_autoincrement int(255)  DEFAULT 1 NOT NULL auto_increment: MySQL error 1067: Invalid default value for 'id_autoincrement '

    For this reason I try to use logic hooks.

    Why does this happen?

Reply
  • I try with this but doesn't work:

    In this path custom\modules\Tasks\Ext\Vardefs\vardefs.ext.php I add this row 

    $dictionary['Task']['fields']['id_autoincrementale_c']['auto_increment']=true;

    But when I try to create a new record in the Tasks module the fields still remain empty or with the default value 1. If I try to change the default value from studio and add the start incrementing value, this erro is shown:

    Cannot modify field Query Failed: ALTER TABLE tasks_cstm modify COLUMN id_autoincrement int(255)  DEFAULT 1 NOT NULL auto_increment: MySQL error 1067: Invalid default value for 'id_autoincrement '

    For this reason I try to use logic hooks.

    Why does this happen?

Children