how-to-add-smart-buttons-in-odoo-18-to-show-sales-orders-on-the-customer-form

How to Add Smart Buttons in Odoo 18 to Show Sales Orders on the Customer Form?

Smart Buttons in Odoo 18

In Odoo 18, user experience continuously improves with small yet effective UI features that enhance efficiency and accessibility. One such feature is Smart Buttons. These neat, clickable buttons are usually located at the top of a form view in Odoo. They provide instant access to related records like viewing a customer’s Sales Orders, Purchase Orders, or Invoices without requiring multiple navigational steps.

In this blog post, we’ll explore how to add a smart button to the Customer form view (res.partner) to show the number of Sales Orders associated with a customer. When the button is clicked, it will open a list of related Sales Orders.

This guide will walk you through the process step by step, whether you’re a developer customizing modules or a tech-savvy business owner looking to streamline your Odoo views.

What are Smart Buttons in Odoo?

Smart Buttons are part of the Odoo form view interface. They appear at the top of the record page and show the number of related documents, such as how many quotations or invoices are linked to a specific partner.

What makes smart buttons truly valuable is that:

  • They are customizable.
  • They provide instant access to related records.
  • They can display dynamic data through computed fields.
  • They improve navigation and data visibility with a click.

By default, Odoo adds smart buttons for common relationships in core apps. However, with a few lines of code, we can add our smart buttons to suit business-specific needs.

Step-by-Step Guide: Add Smart Button for Sales Orders

Step 1: Add a Computed Field to Count Sales Orders

To begin with, we need a computed field that dynamically calculates the number of sales orders linked to a customer. We’ll extend the existing res.partner model and add a field named sale_order_count.

Here’s the Python code to do that:

				
					# models/res_partner.py

from odoo import models, fields, api

class ResPartner(models.Model):
	_inherit = 'res.partner'

	sale_order_count = fields.Integer(string="Sales Orders", compute="_compute_sale_order_count")

	def _compute_sale_order_count(self):
    	for partner in self:
        	partner.sale_order_count = self.env['sale.order'].search_count([('partner_id', '=', partner.id)])

				
			

This method uses search_count() to efficiently count how many sales orders exist for the partner.

Step 2: Add the Smart Button to the Form View

Next, we insert a button inside the oe_button_box section of the customer form view, creating a clickable smart button.

Here’s the XML code to add the smart button:

				
					<!-- views/res_partner_views.xml -->


    res.partner.form.sale.order.button
    res.partner
    
    
        
            <button name="action_view_sale_orders" type="object" class="oe_stat_button">
                <div>
                    
                </div>
            </button>
        
    


				
			

Let’s break this down:

  • The xpath expression finds the correct place to insert the button in the form view.
  • The button uses the oe_stat_button class, which styles it to match other smart buttons.
  • The name attribute references the Python method executed when clicking the button (action_view_sale_orders).
  • fa-shopping-cart is a FontAwesome icon that visually indicates sales.

Step 3: Define the Action to Show Sales Orders

We need to define what happens when the smart button is clicked. In this case, it should open a list view of sales orders filtered by the selected customer.

Add the following method to the res.partner model:

				
					# models/res_partner.py

def action_view_sale_orders(self):
    self.ensure_one()
    return {
        'type': 'ir.actions.act_window',
        'name': 'Sales Orders',
        'view_mode': list,form',
        'res_model': 'sale.order',
        'domain': [('partner_id', '=', self.id)],
        'context': {'default_partner_id': self.id},
        'target': 'current',
    }

				
			

This function uses a standard Odoo act_window action that defines:

  • The model to open (sale.order).
  • The view modes (list and form).
  • A domain filter so only the sales orders for that partner appear.
  • A context that sets the default customer in case a new order is created from this view.

Developer Tips & Additional Notes

Security: Ensure your user has the required access rights to view both res.partner and sale.order records. Otherwise, the smart button may not function correctly.

Extensibility: This approach can be reused for other models, such as account, move (invoices), purchase, order, or even custom models.

Customization: You can change the smart button’s icon or label as needed. Odoo ERP provides several functions that work with Bootstrap-based UI.

Performance: Avoid using .search() with large datasets in the compute method. Stick with .search_count() for better speed and scalability.

Enhancing UX with Smart Buttons

Adding Smart Buttons in Odoo 18 is a simple yet impactful customization that can significantly enhance usability and navigation across form views. In this blog, we explored how to add a smart button to the Customer form that displays the number of related Sales Orders and links directly to their list view.

To summarize:

  • We extended the res.partner model with a computed field to count sales orders.
  • We modified the form view to include a clickable smart button using Odoo’s oe_stat_button style.
  • We added a server method that returns a filtered list of sales orders upon clicking the button.
  • Smart buttons are highly recommended to simplify workflow and reduce user clicks.

Implementing them in your custom modules will not only improve efficiency but also provide a more intuitive user experience.

Picture of Rosmi Thomas

Rosmi Thomas

I’m Rosmi Thomas, an Odoo Developer with a passion for building efficient, customized ERP solutions. I specialize in Odoo module development, integration, and customization, and enjoy exploring open-source tech while sharing insights to support others.

Get a free quote