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?

  • Hi Stefano

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

    Regards

    Sidhu

  • Hi Stefano Mapelli

    You can go with Avinash Singh answer from your autoincrement field.

    Create vardef file and put his code, will solve the problem.

    Regards

    Sam Roy.

  • 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?

  • Hi Stefano Mapelli

    MySQL error coz we can have only one Autoincrement field in a table

    Regards

    Sam Roy.

  • But the table tasks_cstm has only two fields:

    1. id_c CHAR(36) the ID of the Task
    2. id_autoincrement INT(11) the new custom autoincrement field

    What can I do? This solution is not possible

  • Hi Dennis Wangerin

                     Can you please post your comments on this.Hi Stephen we have also checked it but getting the same issue.

    For a quick fix you can do it using logic hooks.

    Regards

    Sidhu

     

  • Remove default value for the field. Auto Increment field can't have a default value. Script should look like

    ALTER TABLE tasks_cstm modify COLUMN id_autoincrement int(255) NOT NULL auto_increment;