Integrated Tech Solutions

How to Create Smart Buttons in Odoo16 Custom Module

By - Admin June 28, 2023

To create a smart button in Odoo, you’ll need to define a method in the corresponding model and link it to a button in the user interface. Here’s a step-by-step guide on how to create a smart button in Odoo:

Step 1: Define a method in the model Create a method in the model where you want to add the smart button. This method will contain the logic for the button’s functionality. For example, let’s say we want to create a smart button in the “sale.order” model that calculates the total amount of the order.

from odoo import models, fields, api

class SaleOrder(models.Model):
    _inherit = 'sale.order'
    
    def calculate_total_amount(self):
        for order in self:
            total_amount = sum(order.order_line.mapped('price_total'))
            order.total_amount = total_amount

    total_amount = fields.Float('Total Amount', compute='calculate_total_amount')

In this example, we’ve added a field total_amount to store the calculated total amount of the order. The calculate_total_amount method calculates the total amount by summing the price_total field from the order_line records associated with the order.

Step 2: Add the button in the user interface To add the smart button to the user interface, you need to modify the corresponding view (e.g., XML file). Locate the view where you want to place the button and add a button element with the appropriate attributes. For example:

<record id="view_sale_order_form_inherit" model="ir.ui.view">
    <field name="name">sale.order.form.inherit</field>
    <field name="model">sale.order</field>
    <field name="inherit_id" ref="sale.view_order_form"/>
    <field name="arch" type="xml">
        <xpath expr="//form/sheet/notebook/page/field[@name='order_line']/tree/field[@name='product_id']" position="after">
            <button name="calculate_total_amount" string="Calculate Total Amount" type="object" class="oe_highlight"/>
        </xpath>
    </field>
</record>

In this example, we’re adding the button after the product_id field in the order line tree view. The button’s name attribute should match the name of the method defined in the model (calculate_total_amount in this case).

Step 3: Update the module After making the changes, update your Odoo module and restart the Odoo server. You can do this by upgrading the module or restarting the server, depending on your deployment.

Once the module is updated and the server is running, the smart button should appear in the specified view. Clicking the button will trigger the calculate_total_amount method and update the total_amount field accordingly.

Note: This is a basic example to demonstrate the concept of a smart button in Odoo. You can customize the logic and appearance of the button according to your specific requirements.

Keep Reading

👋 Hi, Find this Helpful? There is More

You Asked,
We made it!

fix japanese keyword hack

Step by Step Video Tutorials on trending topics in software development

Yes I am Curious »

5 Tips to Pick the Most Engaging Live Chat for Your Website

In today’s fast-paced digital world, providing excellent customer service is a key differentiator for businesses. One of the most effective...

Know More »
next js deployed on aws

Beginners Guide to deploy NextJs App on AWS EC2 Server

In this blog post, we will walk you through the step-by-step process of deploying a Next.js application on an AWS...

Know More »