Use Automated Actions to set income and expense accounts

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:

  1. There is a link between the product template (product.template) and product category (product.category). See below.
  2. Every product must have a product category.
  3. The field names (see below)

Enable “developer” mode and navigate to Settings / Technical / Automated Actions.

Create an Automated Action:

  1. Action Name: Enter a description (e.g. “Set income and expense accounts” )
  2. Model: Product Template (product.template)
  3. Trigger Condition: On Creation
  4. Action To Do: Execute Python Code
  5. 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.

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:

  1. Field type is many2one
  2. Object Relation (link from one model to another) is product.category
  3. 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

7 thoughts on “Use Automated Actions to set income and expense accounts

  1. 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

    Like

    1. 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.

      Like

      1. 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?

        Like

      2. 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?

        Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s