This Automated Action was created as a solution for a problem on the Odoo Help forum (the income and expense accounts for products were being set to incorrect values).
The ‘solution’ is to set the income and expense accounts to the values from the Product Category.
This should not be needed if Odoo behaved as expected (as explained below) but sometimes it’s quicker and easier to do something like this rather than waiting for a fix.
…and it provides an example of what can be done with Automated Actions and Server Actions.
You need to know:
- That there is a link between the product template (product.template) and product category (product.category). See below.
- That every product must have a product category.
- The field names (see below)
Enable “developer” mode and navigate to Settings / Technical / Automated Actions.
Create an Automated Action:
- Action Name: Enter a description (e.g. “Set income and expense accounts” )
- Model: Product Template (product.template)
- Trigger Condition: On Creation
- Action To Do: Execute Python Code
- Python Code (see below for database and field information):
for record in records:
record['property_account_income_id'] = record.categ_id.property_account_income_categ_id
record['property_account_expense_id'] = record.categ_id.property_account_expense_categ_id
It can also be set as a “Contextual Action” so that you can fix incorrect data.
To do that, navigate to Settings / Technical / Server Actions
The Automated Action you created will be shown, so no need to create it again.

Simply click on “Create Contextual Action” and it will be available as an Action in List View and Form View.

It would really be better to set the two accounts to blank so that Odoo can use the accounts from the Product Category, but that seems not to work (again, it is sometimes easier to find a workaround).
- If anyone knows why that doesn’t work (or why the income and expense accounts were being set to incorrect values), please post a comment below.
Database relationship
As mentioned above, there is a link between product template (product.template) and product category (product.category).
We can find it by navigating to Settings / Technical /Fields and enter the filters shown below:

The field is categ_id:

- Field type is many2one
- Object Relation (link from one model to another) is product.category
- The “Field Help” is displayed if you hover your mouse over the field (not in debug mode)

Now we need to find the correct fields on Product Category. Navigate to Settings / Technical /Models and search for product.category:

- property_account_expense_categ_id
- property_account_income_categ_id
Fields to be updated
Enable “developer” mode and hover your mouse over the field to get more information:

The two fields are
- property_account_income_id
- property_account_expense_id
Hi, this is really useful thanks. I came across it whilst trying to figure out if I can update a field on the contact every time I update each of their pos orders. So far I have
model = res.partner
for rec in records:
record[‘partner_id.vat’] = “GB7”
but I receive this error:
alueError: : “name ‘res’ is not defined” while evaluating
‘# Available variables:\n# – env: Odoo Environment on which the action is triggered\n# – model: Odoo Model of the record on which the action is triggered; is a void recordset\n# – record: record on which the action is triggered; may be void\n# – records: recordset of all records on which the action is triggered in multi-mode; may be void\n# – time, datetime, dateutil, timezone: useful Python libraries\n# – log: log(message, level=\’info\’): logging function to record debug information in ir.logging table\n# – Warning: Warning Exception to use with raise\n# To return an action, assign: action = {…}\nmodel = res.partner\nfor rec in records:\n record[\’partner_id.vat\’] = “GB7″‘
Any ideas please? I’m new to Python and worknig it out as I go along. Thanks Mike
LikeLike
It looks as if you have model = res.partner in your Python code. You don’t need it there, you just specify the Model in the definition of the Automated Action.
By the way, I’m not a Python expert. I’m just trying to learn the basics that are required to do simple things like this.
LikeLike
Thanks Chris. I’ve tried that but still no luck. If I change the model to partner (since I want to update a partner field) it then doesn’t let me trigger it from the change on the pos order.
What I’m trying to do is something like:
When pos order created, increment custom field “count of pos orders” on partner. I can’t seem to find a way to get that to work. I’m not sure if it’s even possible to have an action triggered on the pos order to update a field on the related partner?
LikeLike
It’s better to do this with a Computed Field. I’ll try to post something later (probably Friday). What version of Odoo are you using?
LikeLike
brilliant thanks! I’m on 11 Community edition
LikeLike
See if this helps (page still under construction). You can ignore the Odoo Studio part!
https://odootricks.tips/add-a-smart-button-using-odoo-studio/#field
LikeLike
Excellent, thanks Chris, that helps a lot!
Cheers
Mike
LikeLike
NB the code has “for rec in records” then uses the different record object. I think it should use “rec”.
LikeLike
Thanks Giles! Well spotted. I have changed it.
LikeLike
Hi Chris,
How do I copy a custom field value into the new record that is generated by MTO- “replenish on order” from a record that generates that new? or at leat to add the value into the origin or source field? using the server action. Would you please have a clue or write a tutorial for it. Thanks
LikeLike