Skip to Content

Welcome!

human made "AI" (advises & inputs) forum by Odoo professionals, experts and enthusiasts challenging real use cases with standard solutions!

-> solutions should be applicable for Odoo community or online (at the limit with Studio), all without custom modules.

This question has been flagged
2 Replies
11 Views

when creating a product and/or variants without an internal reference, how to automatically add an internal reference (default_code) based on a sequence or ID?

Avatar
Discard

example of solution using the product variant ID (so it works for template and variants) instead of sequence and using prefixes similar to the configurable on the sequences (image from Odoo 18.0):

  • create the following automation rule

# Get the current year (without century) and week of the year
current_year = time.strftime('%y') #e.g.,"25" for 2025
week_of_year = time. strftime('%W') #e.g., "33" for the 33rd week
# Get the product ID
product_id = record.id
# Generate the product code
product_code = "PC" + current_year + week_of_year.zfill(2) + str(product_id)
# Set the product code
record['default_code'] = product_code
Avatar
Discard
Author

create the following sequence:

create the following automation rules (with studio if installed) for the product variant model (to works for variants and templates:

for record in records:
if not record.default_code: # Only generate if not already set
seq = env['ir.sequence'].next_by_code('product.code')
record.write({'default_code': seq})
Avatar
Discard
Related Posts Replies Views Activity
1
Sep 25
226
1
Mar 26
107
1
Aug 25
193
1
Aug 25
153
1
Aug 25
139