In Odoo, actions define the system’s behavior in response to user actions, such as logging in, clicking a button, or selecting an invoice. These actions can be stored in the database or returned directly as dictionaries in methods such as button actions. Odoo provides multiple types of actions, each serving a specific purpose.
Types of Actions in Odoo
1. Window Actions
2. URL Actions
3. Server Actions
4. Client Actions
5. Report Actions
6. Scheduled Actions
Window Actions (ir.actions.act_window)
Window actions are the most common type, used to display views (list, form, kanban, etc.) of models. A window action defines views and model-specific actions.
* type: Specifies the action type, always set to "ir.actions.act_window" for window actions.
* res_model: Defines the model the action operates on.
* views: A list of views available for display when the action is triggered. Each entry consists of a view ID (or False for the default) and the view type (e.g., "form", "list").
* domain: A filter for records displayed by the action, specified as a list of conditions.
* context (optional): Passes additional parameters or default values when the action is triggered.
* name (optional): The label for the action that appears in the UI. Defaults to the model name if not set.
* target (optional): Determines where the action is displayed:
1. current: Same window (default).
2. new: New window.
3. inline: Within the current view.
Example: Opening Customers in List and Form Views
{
"type": "ir.actions.act_window",
"res_model": "res.partner",
"views": [[False, "list"], [False, "form"]],
"domain": [["customer", "=", True]],
}
This action opens the customers list and form views, filtered to show only customers.
URL Actions (ir.actions.act_url)
URL actions allow you to open a URL through an action, either in a new window, the current window, or as a downloadable resource. This is particularly useful for linking to external websites, documents, or files.
1. url: The address to open when activating the action.
2. target (default=new): Specifies how the URL should be opened. The available values are:
* new: Opens the URL in a new window/page.
* self: Opens the URL in the current window/page, replacing the actual content.
* download: Redirects to a download URL.
Example: Opening a Website URL
{
"type": "ir.actions.act_url",
"url": "https://odoo.com",
"target": "self",
}
This action will replace the current page content with the Odoo homepage.
Server Actions (ir.actions.server)
Server actions allow the execution of Python code or the creation of records directly on the server side.
* name: The name of the server action, used for logging and identifying the action in the system logs.
* model_id: The model on which this action will be executed.
* state: Defines the state of the action, typically set to code for executing Python code.
* code: Contains the logic of the action, usually as a call to the model's method or a block of Python code.
Available Actions in Server Actions:
1. Execute Python Code: A block of Python code that will be executed when the action is triggered. This is useful for running custom business logic.
2. Create a New Record: This action allows the creation of a new record with specified values in a given model.
3. Write on a Record: This action updates the values of an existing record based on given criteria.
4. Execute Several Actions: This defines an action that triggers multiple other server actions sequentially, allowing for complex workflows.
Example: Server Action Executing Python Code
<record model="ir.actions.server" id="print_instance">
<field name="name">Res Partner Server Action</field>
<field name="model_id" ref="model_res_partner"/>
<field name="state">code</field>
<field name="code">
raise Warning(record.name)
</field>
</record>
This server action raises a warning with the partner’s name when triggered.
Client Actions (ir.actions.client)
Client actions trigger specific functionality on the client side. They are crucial for implementing features that rely heavily on client-side behavior, such as user interface actions, navigation, or starting specific applications like the Point of Sale (POS) interface.
1. type: Specifies the action type as ir.actions.client.
2. tag: The client-side identifier of the action, which is an arbitrary string that the client knows how to respond to. This is critical for the client-side application to interpret what action to execute.
3. params (optional): A Python dictionary of additional data to send to the client alongside the client action tag. This data can be used to provide context or configurations necessary for executing the action.
4. target (optional): Determines where the client action should be displayed. It can be set to:
* current: Opens the action in the main content area (default behavior).
* fullscreen: Opens the action in full-screen mode.
* new: Opens the action in a dialog/popup window. Using main clears the breadcrumbs.
Example: Starting the POS Interface
{
"type": "ir.actions.client",
"tag": "pos.ui"
}
This action tells the client to start the Point of Sale interface. The server has no knowledge of how the POS interface works; it is entirely client-managed.
Report Actions (ir.actions.report)
Report actions trigger the generation of reports, appearing under the "Print" menu in a model’s views. Report actions are used for generating PDFs, HTML reports, or other formats.
* name: Used as the file name unless print_report_name is specified.
* model: The model your report will be about.
* report_type (default=qweb-pdf): Type of report, either PDF or HTML.
* report_name: The name (external ID) of the QWeb template used to render the report.
* print_report_name: A Python expression defining the name of the report.
* groups_id: A Many2many field for the groups allowed to view/use the current report.
* multi: If set to True, the action will not be displayed on a form view.
* paperformat_id: A Many2one field to the paper format you wish to use for this report (if not specified, the company format will be used).
* attachment_use: If set to True, the report is only generated once the first time it is requested and re-printed from the stored report afterwards instead of being re-generated every time. This is useful for reports that must only be generated once (e.g., for legal reasons).
* attachment: A Python expression that defines the name of the report; the record is accessible as the variable object.
Example: Project Report Action
<record id="action_report_project" model="ir.actions.report">
<field name="name">Project Report</field>
<field name="model">project.project</field>
<field name="report_type">qweb-pdf</field>
<field name="report_name">module_name.project_report_template</field>
<field name="binding_model_id" ref="model_project_project"/>
</record>
This action generates a PDF report for projects and binds it to the project.project model, making it available under the "Print" menu.
Scheduled Actions (ir.cron)
Scheduled actions, also known as automated actions, are triggered automatically at predefined intervals to perform specific tasks, such as sending reports or updating records. These actions are crucial for automating repetitive processes ensuring that certain tasks are executed without manual intervention.
* name: The name of the scheduled action.
* interval_number: Specifies the number of interval_type units between executions.
* interval_type: The unit of measure (e.g., minutes, hours, days).
* model_id: The model on which this action will be executed.
* code: Logic of the action, usually as a call to the model's method.
Example: Sending a Monthly Sales Report
<record id="ir_cron_send_report" model="ir.cron">
<field name="name">Send Monthly Sales Report</field>
<field name="model_id" ref="model_sale_order"/>
<field name="state">code</field>
<field name="code">
model.send_monthly_sales_report()
</field>
<field name="interval_number">1</field>
<field name="interval_type">months</field>
<field name="user_id" ref="base.user_root"/>
<field name="active" eval="True"/>
</record>
This scheduled action triggers the monthly sales report to be sent every month.
Odoo’s versatile action framework allows for the creation of highly dynamic and responsive business applications. From simple window actions for model visualization to complex server-side automation with scheduled actions, each action type serves a specific role, making it easier to customize and adapt Odoo to specific business needs.