Automated Action to prevent duplicates

As standard, Odoo does not have “uniqueness” checks on many fields. This can be problematic, for example if there are

  • multiple products with the same name (which one to choose?)
  • multiple customers or vendors with the same name

Either they are different, in which case they should be given different names, or someone has entered the same data twice.

It is possible to set a field as being unique in the Model definition, but it’s also possible to use Automated Actions.

How to do it

If you are not familiar with Automated Actions, start here

Enable Developer Mode and navigate to Settings / Technical / (Automation) / Automated Actions:

Click on CREATE and enter the following:

Model:  Product Template
Action To Do:    Execute Python Code
Trigger Condition:   On Creation & Update
Before Update Domain: Internal Reference is set
Apply on:  Match all records 

Python Code

if record.default_code:
   existing_product = env['product.template'].search([('id','!=',,('default_code','=',record.default_code)])
   if existing_product:
     raise Warning("You can't have the same Internal Reference Number in Odoo twice!")

3 thoughts on “Automated Action to prevent duplicates

  1. Hi,
    Thanks for the valued python Code, i detect one problem:
    Even with internal references that don’t exist, the program its giving me the message “You can’t have the same Internal Reference Number in Odoo twice!”
    Maybe the problem is solved with an extra line of code but I don’t have the knowledge for it


  2. I believe this technique does work, but maybe there are some special cases that need additional checking.

    Is this always happening? Or only some reference numbers?


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s