Adding A Serial number to a model | Odoo 15 | Automated Actions | Sequences
Create a new sequence/serial number
1. Go to Settings and activate the developer mode
2. Choose Technical from the navigation bar and click on Sequences
3. Create a new sequence by filling out the form as follows:
Implementation: Standard
Sequence code: give it a name - you'll use it later as a reference for this sequence
Prefix: if you want it in "year, month, day" format; you can fill it out like this:
"TSK%(y)s%(month)s%(day)s" | starting with TSK as an index of why is it used
Create a new field
1. Go to the app/model that you want to create the sequence in
2. Click on the bug icon and choose Fields
3. Create a new field
4. Give it a name and a "char" type
5. Save
Save the serial number in the field
1. Go to Settings and choose Technical from the navigation bar
2. Click on Automated actions
3. Create a new automated action by filling out the form as follows:
Model: Name of the model you want to add the sequence to
Trigger: On creation
Action to do: Execute Python code
# write the python code as follows:
if not record.x_serial_number:
record.write({
'x_serial_number' : env['ir.sequence'].next_by_code('TSK')
})
# where:
# x_serial_number is the name of the field added in the model
# TSK is the sequence code
Add the serial number to the model
1. Go back to the app/model
2. Click on the bug icon and choose "Edit: view form"
3. Add the field like this "<field name="name of your field" />"
4. Save!
That’s it, now each new entry of that model will have a unique auto-generated field that starts with TSK followed by the year, month and day
Powered By Hamilton Smart Tech.