Enable Dark Mode!
how-to-add-a-button-inside-the-action-menu-in-odoo-18.jpg
By: Shafna K

How to Add a Button Inside the Action Menu in Odoo 18

Technical

Odoo 18 provides extensive flexibility for developers to customize and enhance user experiences. It allows seamless integration of new features that cater to unique business requirements. One such customization is adding a button inside the Action menu of a model's form view, enabling users to perform specific operations efficiently. In this blog, we’ll walk through the process of creating an Action menu item that updates the order date for selected sale orders, demonstrating the simplicity and power of Odoo's extensible framework.

Step 1: Inheriting the Model

To perform an operation on records, we’ll inherit the sale.order model and define a method that updates the order date.

from odoo import fields, models
class SaleOrder(models.Model):
    _inherit = 'sale.order'
    def action_update_order_date(self):
        for rec in self.browse(self.env.context['active_ids']):
            rec.write({'date_order': fields.Date.today()})

* The action_update_order_date method loops through the selected records (active_ids passed via the context) and updates the date_order field with the current date.

* The browse method is used to fetch the records based on their IDs.

Step 2: Defining the Server Action

Next, we need to create a server action that will trigger the action_update_order_date method. This is done using XML:

<?xml version="1.0" encoding="UTF-8" ?>
<odoo>
    <record id="action_update_order_date" model="ir.actions.server">
        <field name="name">Update Order Date</field>
        <field name="model_id" ref="model_sale_order"/>
        <field name="binding_model_id" ref="sale.model_sale_order"/>
        <field name="binding_view_types">form</field>
        <field name="state">code</field>
        <field name="code">model.action_update_order_date()</field>
    </record>
</odoo>

* ir.actions.server: This is the model used to define server actions in Odoo.

* model_id: Specifies the model on which this action operates (‘sale.order’ in our case).

* binding_model_id: Links the server action to the specified model.

* binding_view_types: Specifies where this action should appear (e.g., in form views).

* state: Defines the action type. Here, code is used to execute Python code.

* code: Executes the method defined earlier (action_update_order_date).

Step 3: Testing the Action

1. Navigate to the Sales module in Odoo.

2. Open any sale order or select multiple sale orders in the list view.

3. Click on the Action menu.

4. You should see the new action labeled “Update Order Date.”

5. When clicked, the action updates the date_order field of the selected records to today’s date.

Benefits of Using Server Actions in the Action Menu

* User-Friendly: Actions can be executed without needing technical knowledge, improving usability for non-technical users.

* Time-Saving: Automates repetitive tasks, reducing manual effort.

* Flexible: Can be customized to handle various operations like updating fields, triggering workflows, or even calling external APIs.

Adding a button to the Action menu is a powerful way to streamline operations in Odoo. By following the steps outlined above, you can easily create custom actions tailored to your business needs. This not only enhances productivity but also demonstrates the flexibility of the Odoo framework.

To read more about How to Add a Button Inside Action Menu in Odoo 17, refer to our blog How to Add a Button Inside Action Menu in Odoo 17.


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



0
Comments



Leave a comment



whatsapp_icon
location

Calicut

Cybrosys Technologies Pvt. Ltd.
Neospace, Kinfra Techno Park
Kakkancherry, Calicut
Kerala, India - 673635

location

Kochi

Cybrosys Technologies Pvt. Ltd.
1st Floor, Thapasya Building,
Infopark, Kakkanad,
Kochi, India - 682030.

location

Bangalore

Cybrosys Techno Solutions
The Estate, 8th Floor,
Dickenson Road,
Bangalore, India - 560042

Send Us A Message