Enable Dark Mode!
a-complete-guide-to-different-types-of-actions-in-odoo-20.jpg
By: Ayana R

A Complete Guide to Different Types of Actions in Odoo 20

Technical Odoo 20 Odoo Community Odoo Enterprises

Actions in Odoo determine how the software behaves when a certain activity is performed by the user. Actions are used to manage navigation between views, run operations in the back-end, show notifications, print reports, and automate business processes. For instance, if a user selects a menu to view customers, chooses an option to edit many records at once, or creates an invoice, all those activities are handled by an action.

There are many models of actions that Odoo 20 offers for various purposes inside the application. The most popular action models are Window Actions, URL Actions, Server Actions, Client Actions, and Report Actions. Besides these action models, there are other action mechanisms provided by Odoo.

In this blog post, we will look into different kinds of actions available in Odoo 20, the purpose they serve, and how to implement them with the help of examples.

1. What Are Actions in Odoo 20?

Actions in Odoo are commands that specify how the system is supposed to behave when an operation is executed. Actions act as an interface connecting user actions and operations carried out in the Odoo framework.

An action may either be saved in the database as a record or returned as a Python dictionary object from the method of the model. The Odoo web client takes the action and executes the operation.

An action typically has two important fields:

  • Type: Indicates what type of action it is and how Odoo will handle it.
  • Name: Identifies the action through its name.

The actions may be launched in the following ways, among others:

  • Click on a menu item.
  • Click on a button in the form or list view.
  • Choose an action from the Action menu.
  • Choose an action from the Action menu.
  • Automated or scheduled execution.

For instance, when a user clicks the Customers menu in the Contacts application in Odoo, a window action is executed to show customer entries.

2. Different Types of Actions in Odoo 20

There are five main types of Odoo actions.

  • Window Actions
  • URL Actions
  • Server Actions
  • Client Actions
  • Report Actions

Now, let’s discuss each of them through examples.

2.1. Window Actions (ir.actions.act_window)

Actions for windows are one of the most frequently used actions in Odoo. They help open views of a specific model.

For instance, if a user selects the Customers menu, Odoo shows customers in a list view. After selecting a customer, the form view opens. It is controlled using a window action.

Various types of views may be opened by window actions, including:

  • List View
  • Form View
  • Kanban View
  • Calendar View
  • Graph View
  • Pivot View
  • Activity View

Example: Window Action

The below-given XML code is used to create a window action to open customer records.

<record id="action_customer_list" model="ir.actions.act_window">
    <field name="name">Customers</field>
    <field name="res_model">res.partner</field>
    <field name="view_mode">list,form</field>
</record>

Description of the code:

The res.partner object is used to display contacts. The view_mode field indicates that the user can use List and Form views to access the records.

Creating a window action using Python

Window actions can also be created in Python functions.

def action_open_customers(self):
    return {
        'type': 'ir.actions.act_window',
        'name': 'Customers',
        'res_model': 'res.partner',
        'view_mode': 'list,form',
    }

When this process is done, Odoo will display the customer’s records in the list view and form view.

When to Use Window Actions?

  • When displaying records using a custom menu.
  • When showing related records on a form view.
  • When displaying filtered records using domains.
  • When navigating a user to a specific record/model.

2.2. URL Actions (ir.actions.act_url)

URL Actions are used for opening a particular website or web page from Odoo. This feature is helpful when users want to access any external website, documents, payment pages, or any web pages from the Odoo application.

Example: Creating a URL Action

The following is an example of creating an action to open the official website of Odoo.

<record id="action_open_odoo_website" model="ir.actions.act_url">
    <field name="name">Odoo Website</field>
    <field name="url">https://www.odoo.com</field>
    <field name="target">new</field>
</record>

Code Explanation:

  • Name: This defines the name of the action.
  • URL: This identifies the website or URL that is to be launched.
  • Target: This decides the way the URL is to be launched.
  • Returning a URL Action from Python

A URL action can also be returned from a Python function.

def action_open_odoo(self):
    return {
        'type': 'ir.actions.act_url',
        'url': 'https://www.odoo.com',
        'target': 'new',
    }

The execution of this action involves opening the website in a new browser tab or window by the Odoo client.

URL Actions

When to use URL Actions?

  • Opening of external websites
  • Redirecting user to payment pages
  • Referencing of external documents
  • Opening of web sources in Odoo

2.3. Server Actions (ir.actions.server)

The purpose of the server actions is the execution of some server-side operations. Using the server actions developers and functional administrators can run backend operations without creating a new Python function for each small task.

They can be configured via the Odoo interface or XML.

Also, they can be used to update records, create new ones, run Python code and multiple server actions.

Odoo provides different server action states , for example:

  • Run Python Script: Runs a Python script in the server.
  • Create Record: Creates a record in the designated model.
  • Update Record: Modifies values in the existing record.
  • Duplicate Record: Duplicates the existing record.
  • Webhook Notification: Sends notification to the external system via webhook.
  • Multiple Actions: Carries out multiple actions in the server in the defined order.

Keep in mind that this is the actual technical state value for Odoo 20, and depending on your configuration and modules installed, you may have different choices available.

Example: Creating a Server Action

This XML code below defines a server action with name Check Status for the model Batch Payments (account.batch.payment). It is defined in the account_online_payment module which is an enterprise edition feature. This action is accessible through List and Kanban Views and when the user selects the batch payment record(s) and performs the action then Odoo invokes the function check_online_payment_status().

<record model="ir.actions.server" id="action_account_online_payment_check_status">
        <field name="name">Check Status</field>
        <field name="model_id" ref="model_account_batch_payment"/>
        <field name="binding_model_id" ref="model_account_batch_payment"/>
        <field name="binding_view_types">list,kanban</field>
        <field name="state">code</field>
        <field name="code">
            if records:
                action = records.check_online_payment_status()
        </field>
</record>

Code explanation:

  • name: The name of the action of the server
  • model_id: Specifies the name of the model the action will run on
  • binding_model_id: This makes the action appear in the action menu of the model specified
  • binding_view_types: The views where the action will appear. In this case, it is List and Kanban.
  • state: Specifies the execution type of the action. The code execution type will cause python code to be executed.
  • code: Runs the check_online_payment_status() method on the selected batch payment records

Server Action Usage Scenarios:

  • Update more than one record at a time.
  • Create records automatically.
  • Run the business logic from the Actions menu.
  • Do backend tasks without including specific buttons.
  • Group server-side tasks together.

2.4. Client Actions (ir.actions.client)

Actions are performed at the client end in Odoo. As against server actions that run server side operations, client actions run in the Odoo web client.

These actions are typically helpful to show notifications, open custom client actions, and to provide interactive front-end functionality.

Client actions usually are referred to as a tag. The web client in Odoo makes use of the tag to identify which client actions are to be performed.

Example: Showing a Notification via Client Action

There is a client action in Odoo that shows notifications. Its name is display_notification.

This is a Python function to return client action that would show a success notification.

def action_show_notification(self):
    return {
        'type': 'ir.actions.client',
        'tag': 'display_notification',
        'params': {
            'title': 'Success',
            'message': 'Operation completed successfully!',
            'type': 'success',
            'sticky': False,
        }
    }

Explanation of the code:

  • type: Declares that the action returned by the method is a client action.
  • tag: Refers to the client-side action to be performed. In this case, it refers to the built-in notification action provided by Odoo.
  • params: Contains extra data that will be passed to the notification.
  • title: Provides the title for the notification.
  • message: Provides the message shown to the user.
  • type: Declares the type of notification; e.g., success, warning, danger.
  • sticky: Indicates whether the notification will be sticky.

Execution of this method results in displaying a success notification by the Odoo web client.

Client Actions Customization

It is possible to write your own client actions in JavaScript using the Owl framework.

As an example, a custom dashboard can be added to the Odoo action registry and opened via an ir.actions.client action record.

XML example:

<record id="action_custom_dashboard" model="ir.actions.client">
    <field name="name">Custom Dashboard</field>
    <field name="tag">my_module.custom_dashboard</field>
</record>

The JS part should be registered by the proper action registry key for the tag my_module.custom_dashboard.

This will enable developers to develop custom dashboards and interfaces in Odoo.

When to use Client Actions?

  • Displaying notifications.
  • Development of custom dashboards.
  • Implementation of custom frontend elements.
  • Adding custom client-side functionality.
  • Operations that need the Odoo web client.

2.5. Report Actions (ir.actions.report)

Report actions are needed to generate reports in Odoo. Report actions allow users to print or download the document in business applications in supported formats, e.g., PDF.

For instance, when printing a sales order, invoice or quotation, Odoo will create a document through a report action.

The most common way Odoo builds PDF reports is by using QWeb templates.

Example: Developing a Customer PDF Report

Firstly, create the report action by XML.

<record id="action_customer_report"
        model="ir.actions.report">
    <field name="name">Customer Report</field>
    <field name="model">res.partner</field>
    <field name="report_type">qweb-pdf</field>
    <field name="report_name">action_demo.customer_report</field>
    <field name="binding_model_id" ref="base.model_res_partner"/>
    <field name="binding_type">report</field>
</record>

QWeb report template:

<template id="customer_report">
    <t t-call="web.html_container">
        <t t-foreach="docs" t-as="customer">
            <div class="page">
                <h2>Customer Report</h2>
                <p>
                    Customer Name:
                    <span t-field="customer.name"/>
                </p>
                <p>
                    Email:
                    <span t-field="customer.email"/>
                </p>
            </div>
        </t>
    </t>
</template>
  • model: Identifies the model for which the report is generated.
  • report_type: Specifies the report format.
  • report_name: References the QWeb template using its complete external ID.
  • binding_model_id: Makes the report available in the Print menu of the Contacts model.
  • binding_type: Identifies the binding as a report.

When to use Report Actions?

  • PDF Document Generation
  • Printing Invoices & Quotations
  • Creation of Custom Business Reports
  • Exportation of Formatted Business Data
  • Document Creation using Selected Records

3. Other Automation Mechanisms in Odoo 20

Besides the five major types of actions, there are also automation capabilities that are available in Odoo that allow performing certain operations automatically for businesses.

Two crucial notions in this regard are Automated Actions and Scheduled Actions.

Both are not types of actions in terms of the ir.actions.* category but are separate capabilities for automating operations in business.

3.1. Automated Actions

Actions that are automated enable users to run actions automatically if certain criteria or triggers occur. Automated actions are helpful when a company has to run an action, and it is not necessary for users to press a button or run an action manually.

The automation actions can be set up via the Technical menu by using Automation Rules based on users' permissions and developer configurations.

Common Automated Action Triggers Available in Odoo 20:

  • Creation Trigger: When triggered by the creation of a record.
  • Update Trigger: When triggered by the update of an existing record.
  • Creation or Update Trigger: When triggered by the creation or update of a record.
  • Deletion Trigger: When triggered by the deletion of a record.
  • Date-Based Trigger: When triggered based on a specified date or datetime field.
  • User Interface Trigger: When triggered by the change of a field in the user interface.

There are also several specific automated action triggers available in Odoo 20, including stage changes, priority changes, tag changes, and webhooks.

Example: Automatically Updating a Customer Comment

Consider a requirement where a comment should be added automatically whenever a new customer record is created.

Create an automation rule with the following configuration:

  • Model: Contact (res.partner)
  • Trigger: On Creation
  • Action To Do: Execute Code

Python code:

if record:
    record.write({
        'comment': 'Customer created automatically'
    })

Explanations:

  • record indicates the record that fired the automation.
  • write() method changes the value of the specified field.
  • The process runs automatically based on the occurrence of the defined trigger.

When a customer is created, the automated rule triggers and changes the value of the comment field.

When should automated actions be used?

  • Changing records based on some conditions.
  • Notifications after changing records.
  • Follow-up creation.
  • Automation of approval processes.
  • Execution of business rules without human intervention.

3.2. Scheduled Actions (ir.cron)

Scheduled actions make Odoo able to perform certain actions automatically in specified periods of time.

Such actions are often used for performing certain background processes that must run periodically, like sending reminders, updating records, checking deadlines, and syncing data.

Scheduled actions are created with the help of the ir.cron model.

Example: Define the scheduled action in XML.

<record id="hr_leave_cron_cancel_invalid" model="ir.cron">
        <field name="name">Time Off: Cancel invalid leaves</field>
        <field name="model_id" ref="model_hr_leave"/>
        <field name="state">code</field>
        <field name="code">model._cancel_invalid_leaves()</field>
        <field name="interval_number">1</field>
        <field name="interval_type">days</field>
    </record>

The following XML code defines a scheduled action for the Time Off module. It executes the _cancel_invalid_leaves() method of the hr.leave model once every day. This scheduled operation is used to process invalid leave records automatically without requiring manual intervention in the timeoff module.

When to use Scheduled Actions?

  • Send automatic reminders.
  • Update records regularly.
  • Synchronize data among systems.
  • Check pending transactions.
  • Run regular background processes.

4. Difference Between the Different Action Types in Odoo 20

Actions have different purposes. Recognizing these differences enables programmers to pick the right action for a certain need.

CategoryTechnical modelPurpose
Window Actionir.actions.act_windowOpens model views
URL Actionir.actions.act_urlOpens URLs
Server Actionir.actions.serverExecutes backend operations
Client Actionir.actions.clientHandles client-side operations
Report Actionir.actions.reportGenerates reports
Automated Actionbase.automationExecutes operations based on triggers
Scheduled Actionir.cronExecutes operations periodically

Actions play a crucial role in the Odoo framework, as they control how various operations are carried out depending on user actions or events.

Odoo 20 offers a variety of actions that can be used to facilitate navigation, backend operations, frontend features, report creation, and opening external URLs. Apart from these major types of actions, there are automated and scheduled actions that assist enterprises in saving time through the automation of processes using certain triggers or schedules.

Knowing what each type of action is intended for, Odoo developers and functional consultants can determine the correct method to use when customizing the business workflow. Using actions, along with Python methods, XML, and the Odoo web client, developers are able to create solutions that suit various business needs.

To read more about The Ultimate Guide to Action Types in Odoo 19, refer to our blog The Ultimate Guide to Action Types in Odoo 19.


Frequently Asked Questions

How does a Window Action differ from a Server Action?

Window Actions are responsible for displaying views related to a certain model, while a Server Action performs operations on the backend, for example, creating, updating, or processing records.

Is it possible to execute Python code using a Server Action in Odoo 20?

Yes, it is. A Server Action may perform the "Execute Python Code" operation, which means that the statements will be executed in a sandboxed environment with Odoo execution variables available.

What is a Client Action used for in Odoo?

A Client Action is used to perform client-side operations in the Odoo web client, like displaying notifications, opening custom dashboards, and executing custom frontend components.

If you need any assistance in odoo, we are online, please chat with us.



0
Comments



Leave a comment



WhatsApp