Enable Dark Mode!
how-to-create-a-new-embed-action-in-odoo-19.jpg
By: Ruksana P

How to Create a New Embed Action in Odoo 19

Technical Odoo 19 Odoo Community Odoo Enterprises

In Odoo 19, the Project app provides a powerful and flexible way to manage project-related activities efficiently. One of its most useful features is the top bar, which gives users quick access to important records and documents linked to a project.

How to Create a New Embed Action in Odoo 19-cybrosys

The top bar contains important and needy item such as the dashboard, sales orders, invoices, and other related documents, all accessible directly from the project view. Instead of switching between multiple menus or apps, users can instantly navigate to what they need.

In this blog, we are discussing how to add a new embedded action to the project top bar to see the purchase material for this project.

1. Creating the Embedded Action

First, we need to create a new embedded action record in XML. This record connects the Project model with a Python method that dynamically returns the required product view.

<?xml version="1.0" encoding="utf-8" ?>
<odoo>
 <record id="project_embedded_action_project_purchased_product" model="ir.embedded.actions">
   <field name="parent_res_model">project.project</field>
   <field name="sequence">20</field>
   <field name="name">Purchased Products</field>
   <field name="parent_action_id"  
     ref="project.act_project_project_2_project_task_all"/>
   <field name="action_id" eval="False"/>
   <field name="python_method">action_get_products</field>
   <field name="domain">[('task_ids', '!=', False)]</field>
   <field name="groups_ids" eval="[(4, ref('product.group_product_variant'))]"/>
   <field name="context">
         {'from_embedded_action': true}</field>
   </record>
</odoo>
  • parent_res_model: The embedded action is attached to the Project model.
  • sequence: display order of the action in the top bar.
  • name: This is the name shown to users in the Project top bar.
  • parent_action_id: This links the embedded action to the Project’s main action view.
  • action_id and python_method: Define the either action_id or python_method.
  • default_view_mode: If the action is set, then we can define the view mode. If it is not set, the default view of the action is taken.
  • domain: This controls visibility of this embedded action on the top bar.
  • groups_ids: This restricts visibility of the action to users who belong to the Product Variant group, ensuring proper access control.
  • Context: We can pass the params to the backend method to differentiate embedded action calls from normal actions. It helps control behavior such as view switching or UI flow.

2. Define Python method

We extend the Project model in Odoo 19 to introduce a custom smart action that allows users to quickly access purchased products related to a project.

# -*- coding: utf-8 -*-
from odoo import models
from odoo.tools.translate import _

class ProjectProject(models.Model):
   _inherit = "project.project"
   def action_get_products(self):
       self.ensure_one()
       purchase_lines = 
          self.env['purchase.order.line'].search([
           '|', ('analytic_distribution', 'in',     
            self.account_id.ids),
           ('order_id.project_id', '=', self.id)])
       products = purchase_lines.mapped('product_id')
       action = {
           'name': self.env._('Products'),
           'type': 'ir.actions.act_window',
           'res_model': 'product.product',
           'view_mode': 'kanban, list, form',
           'views': [             (self.env.ref('product.product_kanban_view').id, 'kanban'),
               (False, 'list'), (False, 'form'),],
           'domain': [('id', 'in', products.ids)],
           'context': dict(self.env.context),
       }
       if len(products) == 1 and  
           self.env.context.get('from_embedded_action'):
           action.update({
               'view_mode': 'form',
               'res_id': products.id,
               'views': [(False, 'form')],
           })
       return action

This method provides a way for users to view all purchase-related products directly from the Project interface without navigating through multiple pages.

How to Create a New Embed Action in Odoo 19-cybrosys

By clicking the purchased product button, we get all the purchased products for this project.

How to Create a New Embed Action in Odoo 19-cybrosys

The top bar is fully customizable for each project. This allows teams to define it according to their workflow requirements, ensuring that only the most relevant resources are displayed. As a result, project management becomes faster, more organized, and highly efficient within the Odoo environment.

To read more about The Ultimate Guide to Action Types in Odoo 19, refer to our blog The Ultimate Guide to Action Types in Odoo 19.


Frequently Asked Questions

How to add a new button to the top bar of the project in Odoo 19?

To add a new button, you define an embedded action linked to the Project model. This action appears automatically in the Project top bar when conditions are met.

How to define a new embedded action in Odoo 19?

To define a new embedded action in Odoo 19, you create an entry in ir.embedded.actions and link it to a parent model. This allows you to add a quick action in the top bar or inside a parent view, which can execute a Python method or open an existing action.

What is an embedded action in Odoo 19?

An embedded action is a quick-access feature added inside a parent view (such as Project) that allows users to open related data or execute custom logic directly from the interface.

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



0
Comments



Leave a comment



WhatsApp