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?