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 was 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.
- 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 rec 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 / Automated 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.
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:
Fields to be updated
Enable “developer” mode and hover your mouse over the field to get more information:
The two fields are