In Odoo 18, the Discuss module plays a crucial role in facilitating communication and collaboration within the system. The Chatter is an essential feature integrated into various Odoo modules like CRM, Sales, and Project Management, enabling users to communicate and share information directly within records. To enhance the user experience and extend the capabilities of Chatter messages, Odoo provides flexibility in introducing custom actions.
Custom actions empower users to perform specific operations or trigger workflows directly from Chatter messages. These actions can be tailored to meet the unique requirements of a business or organization using Odoo. The following guide explains how developers can leverage Odoo 18's extensibility features to add a custom action within Chatter messages.

Patching Message Class And Add Function
The patch function is used to extend the functionality of the Message class, allowing developers to introduce additional methods and properties. In the example below, a function named createLead is added to the Message class. This function creates a lead from the message text.
/** @odoo-module **/
import { patch } from "@web/core/utils/patch";
import { useService } from "@web/core/utils/hooks";
import { Message } from "@mail/core/common/message";
patch(Message.prototype, {
setup() {
super.setup();
this.orm = useService("orm");
this.actionService = useService("action");
},
async createLead() {
if (!this.message.id) {
console.error("Message ID is missing!");
return;
}
const messageId = this.message.id;
const data = await this.orm.call("mail.message", "action_create_lead", [[messageId], {}]);
this.actionService.doAction({
res_model: "crm.lead",
res_id: data,
target: "current",
type: "ir.actions.act_window",
views: [[false, "form"]],
});
},
});
The Python function provided below is responsible for extracting relevant details from a Chatter message and using that information to create a new lead in the CRM module. It retrieves key attributes such as the message subject, body content, and sender’s email address, ensuring that the lead is populated with accurate and meaningful data. This automation helps streamline the lead generation process by converting discussions into actionable sales opportunities.
Once the function is implemented, the next step is to register this action within the Odoo system. Registering the action ensures that users can trigger it directly from the Chatter interface with a simple click.
import re
from odoo import models
class MailMessage(models.Model):
_inherit = "mail.message"
def action_create_lead(self,msg):
"""Create a lead from the mail message"""
self.ensure_one()
body_text = self._strip_html_tags(self.body)
lead_vals = {
"name": body_text or "New Lead",
"type": "lead",
"user_id": self.env.user.id,
"partner_id": self.author_id.id if self.author_id else False,
"contact_name": self.author_id.name if self.author_id else "",
"email_from": self.email_from or "",
}
lead = self.env["crm.lead"].create(lead_vals)
return lead.id
def _strip_html_tags(self, html):
"""Remove all HTML tags using regex"""
if not html:
return ""
return re.sub(r"<.*?>", "", html).strip()
Registering Message Action
/* @odoo-module */
import { _t } from "@web/core/l10n/translation";
import { messageActionsRegistry } from "@mail/core/common/message_actions";
messageActionsRegistry.add("create_lead", {
condition: (component) => component.props.message.canReplyTo(component.props.thread),
icon: "fa fa-check",
title: () => _t("Convert to Lead"),
onClick: (component) => component.createLead(),
sequence: 0,
})
Action Configuration:
* condition:(component)=>component.props.message.canReplyTo(component.props.thread): Specifies a condition under which the action should be visible. In this case, it depends on the canReplyTo property of the component.
* icon: "fa fa-check": Sets the icon for the action. Here, it's using the Font Awesome icon "fa-check."
* title: () => _t("Convert to Lead"): Defines the title of the action, which is translated using the _t function. The title is "Convert to Lead."
* onClick: (component) => component.createLead(): Specifies the function to be executed when the action is clicked. In this case, it calls the createLead method on the component.
* sequence: 0: Sets the sequence or priority of the action. Actions with lower sequence numbers appear first.
Conclusion

Odoo 18 offers a powerful and flexible way to customize Chatter messages by introducing custom actions. By extending the Message class and registering actions properly, developers can create intuitive and efficient workflows within the Discuss module. The example of adding a "Convert to Lead" action demonstrates how businesses can leverage Odoo's extensibility to enhance communication and operational efficiency.
With the ability to streamline workflows and enhance user engagement, custom actions in the Chatter module make Odoo 18 a more dynamic and adaptable platform for collaboration and productivity.
To read more about How to Add Action in Discuss Chatter Messages in Odoo 17, refer to our blog How to Add Action in Discuss Chatter Messages in Odoo 17.