Automated Actions

Automated Actions (and their close cousin, Server Actions) are a powerful way to add functionality to Odoo without any programming (or with only a few lines of Python code).

Also, if you are using Odoo Online you cannot do development or install third-party add-ons but you can use Automated Actions (and Server Actions).

Automated Actions can be used to enhance and customize Odoo functionality in several ways:

Automated Actions can be triggered on Creation, Deletion or Update of a database record in the specified Odoo Model (database table), and can also be based on Form Modification or a Timed Condition.

Automated Actions can be conditioned on domains and / or “watched fields”, which makes it possible to define “business rules” (an action for one group of customers, or one type of product, etc.) and more complex logic can be added with Python code.

Actions

  1. Update fields on the selected model (database table)
    1. Set a fixed value (can be based on rules)
    2. Derive a new value:
    3. Clear a field
  2. Create new records on another table
  3. Send email (a useful feature, but email templates can be tricky to setup)
  4. Add followers (example on Odoo Help Forum)
  5. Create activities (so a user can follow-up)
  6. Validation (needs Python)
  7. Customize document numbering (needs Python)
  8. Other Python functions
    1. Copy Records
    2. Sort data
  9. It’s also possible to execute multiple server actions (for the same Model)

Getting started

Start by enabling developer mode and navigating to Settings / Technical / Automated Actions.

Note: Every Automated Action is also a Server Action, which enables it to be added as a “contextual action” (example).

If you cannot find this option, you may need to install the module “Automated Action Rules”.

First remove the “Apps” filter by clicking on the ‘X’:

Then search for “Automated”

If it’s not installed, click on ‘INSTALL’.

Now you might want to try one of the examples (links above). Or if you want to understand more about Automated Actions, read on:

Automated Action

Model

Select the Model (database table)

Action To Do

  • Execute Python Code (see below)                             
  • Create                                          
    • Create a new record in a specified model (db table). You specify the fields in the ‘Data to Write‘ tab.
  • Update the (current) Record                  
  • Execute several actions         
    • Trigger several other server actions
  • Send Email                                 
    • Automatically send an email (using a template)
  • Add Followers                           
  • Create Next Activity                                

Trigger Condition

This defines when the Automated Action can be triggered:

  1. On Creation [on_create]
  2. On Update [on_write]
  3. On Creation & Update [on_create_or_write]
  4. On Deletion [on_unlink]
  5. Based on Form Modification [on_change]
  6. Based on Timed Condition

Watched Fields

This was added in Odoo 13

This is optional, but if you specify any fields here the Automated Action will only be triggered if the value of these field(s) have changed.

Domains

For the first four trigger conditions, you can define the records to be selected using domains. Odoo will show you how many records meet the criteria and you can display a list of records to check whether your criteria is correct.

There are two domains:

  1. Before Update Domain (used for Updates only)
  2. Apply on

So it is possible to have “before” and “after” domains to trigger an action when a user makes a specific change (e.g. adding a salesperson to a customer – the field was blank before and now has a value).

Note that there are some limitations with domains if you create Automated Actions through the ‘front-end’ menu.

  • There is a pop-up dialog box which makes it easier to enter domains, but at the cost of limiting functionality.

Data to Write

The simplest way to update or write data is to select the fields from a list. This applies when the action is “Create” or “Update the Record”

Simple Python expressions can also be used

The first two lines are taking data from the record, the third line writes multiple (fixed) values to a many2many field, the last line takes data from the parent record. This is from the Add contacts to mailing lists example.

Python Code

Automated Actions can use a subset of Python

  • env: Odoo Environment (gives access to information about current user and current company, as per this example)
  • model: Odoo Model (database table)
  • record: gives access to field values, e.g. record['company_id']
  • records: allows multiple records to be read / updated, as per this example
  • timedatetimedateutiltimezone: to retrieve date and time
  • log(message, level='info'): to record debug information in ir.logging table
  • Warning: Display a Message (as per this example)
  • action = {...}

There’s no need to understand everything that can be done using Python. In fact, you can create Automated Actions without any Python at all.

Set customer lead time

  • Model = product.template
  • Trigger Condition = On Creation
  • Apply On (domain) = Can be Sold
    • Note that Odoo tells you that 94 records meet the criteria (and you can click here to view a list of those records)
  • Action To Do = Update the Record
  • Data to Write
    • Field = Customer Lead Time
    • Evaluation Type = Value
    • Value = 10

So we can see that using “Update the Record” is quite simple and requires only a basic knowledge of the Odoo database (but user-defined defaults can also be used to do something similar).

However, using simple Python expressions allows us to go beyond what can be done with user-defined defaults. Here we can use values from the current record, from a parent (e.g. update Sales Order Line with information from the Sales Order Header), from related Models (db tables) and environment variables.

Other Examples

This is a quite powerful function. It’s possible to create analytic accounts for sales orders, which replicates the standard functionality in Odoo to automatically create an Analytic Account for each project.

  • This example also contains some information about how to overcome a bug in Automated Actions.

There will be other business cases where this could be a good technique.

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