Adding Smart Buttons in Odoo 17: A Guide

Smart Buttons in Odoo serve as interactive elements strategically positioned within records, providing users with a swift and contextual method to execute associated actions or access insights without needing to navigate away from the current view. These buttons cleverly gather and exhibit pertinent information, converting static data display into an engaging and dynamic user experience.

In Odoo 17, the smart button features a new sleek appearance. Below is the contacts form view showcasing several smart buttons. Now, let's explore how to incorporate smart buttons into the Odoo 17 Module.

Within this Joel Willis contact form, you'll find smart buttons for Meetings, Tasks, and Invoices, each accompanied by an icon and the corresponding record count. Now, we'll proceed to include a new smart button on this form, showcasing the assigned vehicle record for this partner from the fleet module.

Incorporating a smart button into the form view

To include a button alongside other smart buttons, we need to inherit the form view and insert the button within a <div> labeled "button_box".

    <record id="view_partner_form" model="ir.ui.view">
        <field name="name">res.partner.form.inherit.my.blog</field>
        <field name="model">res.partner</field>
        <field name="inherit_id" ref="base.view_partner_form"/>
        <field name="arch" type="xml">
            <div name="button_box" position="inside">
                <button class="oe_stat_button" 
                         type="object" 
                         icon="fa-taxi"                         
                         name="action_get_vehicles_record>
                </button>
            </div>
        </field>   
</record>

Upon adding this record and integrating the function "action_get_vehicles_record" specified within the record on the res.partner class, a new smart button will be introduced in the partner form, featuring an icon of a taxi (fa-taxi).

Title and information for the smart button

Now, we can include titles for the buttons and display record counts on this newly added smart button.

To achieve that, extend the res.partner model and introduce a field along with a compute function as demonstrated below.

class ResPartner(models.Model):
    _inherit = "res.partner"
    vehicle_count = fields.Integer(string="Vehicles",
                                   compute='compute_vehicle_count',
                                   default=0)
    def compute_vehicle_count(self):
        for record in self:
            record.vehicle_count = self.env['fleet.vehicle'].search_count(
                [('driver_id', '=', self.id)])


Ensure this data is accessible on the smart button and revise your XML record to include the vehicle_count field within the button tag.

<div name="button_box" position="inside">
    <button class="oe_stat_button" type="object" icon="fa-taxi"
            name="action_get_vehicles_record>
        <field string="Vehicles" name="vehicle_count" widget="statinfo"/>
    </button>
</div>

Subsequently, you'll be able to observe the smart button label and the vehicle count.

Specify the action for the smart button.

We can now establish the action for the smart button by refining the "action_get_vehicles_record" function to filter the view of vehicle records assigned to the partner.

 def action_get_vehicles_record(self):
        self.ensure_one()
        return {
            'type': 'ir.actions.act_window',
            'name': 'Vehicles',
            'view_mode': 'tree',
            'res_model': 'fleet.vehicle',
            'domain': [('driver_id', '=', self.id)],
            'context': "{'create': False}"
        }


Assign the current partner as a vehicle driver in the Fleet module, then click on the smart button.

The displayed view of related vehicles will appear as depicted in the image below.

In Odoo 17, Smart Buttons elevate user experience by providing a streamlined approach to execute actions and access pertinent information within record views. This enhances usability and injects a dynamic aspect into data presentation.

A Summary of Product Variant Management using Odoo 17 POS