Skip to Content
Menu
This question has been flagged
1 Reply
3369 Views

My weekly imports use an External ID in the form "barcode.XXXX" where XXXX is the barcode of the product.

Example:

Week 1: Product with External ID "barcode.1234" is created with price $5

Week 2: Product with External ID "barcode.1234 is UPDATED to price $6


Note that the barcode field is also updated with "1234".

As long as a user doesn't create products, I am able to update and/or add new products each week.

If users notice that a new product isn't yet in Odoo, they will create it.  Since Odoo doesn't assign an External ID, when the import runs, a DUPLICATE of this product will be created.

Example:

Between Week 2 and Week 3: Product with barcode 5678 is created by a user. External ID does not exist.

Week 3: Product with External ID "barcode.5678" is created - a DUPLICATE.


How can I setup Odoo so that when a user creates a Product, an External ID matching the format I am using is automatically created?


Avatar
Discard
Best Answer

Assuming you are managing the contents of the barcode field (to prevent duplicates or null values for example) you can do something like this via an Automated Action that runs on create and update of the Product Template:

product_barcode = record.barcode
env.cr.execute("SELECT * FROM ir_model_data WHERE module=%s and name=%s",                      
("barcode", product_barcode))
if env.cr.rowcount == 0: 
env.cr.execute(""" INSERT INTO ir_model_data (module, name, model, res_id, date_init, date_update)                              
VALUES (%s, %s, %s, %s, (now() at time zone 'UTC'), (now() at time zone 'UTC')) """, 
                  ("barcode", product_barcode, "product.template", record.id))

Note that this is the same way Odoo checks for and assigns External ID's during the import process:

\https://github.com/odoo/odoo/blob/11.0/odoo/addons/base/ir/ir_model.py#L257

XML ID UPDATE

Avatar
Discard
Related Posts Replies Views Activity
1
Mar 23
1212
0
Oct 21
6677
5
Apr 18
6701
2
Mar 15
11631
1
Aug 24
355