How to Create Display Notification in Odoo 17

blog-banner

In any business management software, effective communication between users is crucial for smooth operations. Odoo, a comprehensive suite of business applications, provides robust features for managing communication within its framework. One such feature is the ability to create display notifications, which enhance user experience by alerting them about important messages or updates.

In this blog post, we will explore how to implement display notifications in Odoo 17 using Python code. We'll cover the creation of private channels and sending message notifications to users.

Use Case

Imagine you're running a manufacturing company using Odoo 17 for managing sales orders. In your workflow, sales orders require approval from designated approvers before they can be processed further. To streamline this approval process and keep all stakeholders informed, you can leverage display notifications.

Benefit to Users

Efficient Communication: With display notifications, approvers receive instant updates about pending approval requests directly within Odoo, eliminating the need for manual communication via emails or messages.

Improved Collaboration: By creating private channels for each approval request, users can discuss specific details or provide feedback within the context of the sales order, fostering collaboration and ensuring clarity.

Time saving: Automating notification delivery reduces the time spent on manual follow-ups, allowing users to focus on critical tasks and accelerating decision-making processes.

Enhanced Visibility: All communication related to approval requests is centralized within Odoo, providing a transparent audit trail for tracking approvals and maintaining accountability.

Python Code

Creating Private Channel

Private channels in Odoo serve as discussion groups between specific users. These channels are particularly useful for one-to-one communication or for discussing sensitive information. Below is the Python code to create a private channel between two users:

from odoo import models 

class Channel(models.Model): 

    """ A 'discuss.channel' is a discussion group that may behave like a listener 

    on documents. """ 

    _inherit = 'discuss.channel' 

    def create_private_channel(self, user1, user2): 

        """ 

        This method is used for creating a new channel. 

        """ 

        channel = self.env['discuss.channel'].create({ 

            'name': user1.name +','+ user2.name, 

            'channel_type': 'chat', 

            'channel_partner_ids': [ 

                (4, user1.partner_id.id), 

                (4, user2.partner_id.id)] 

        }) 

        return channel 

Sending Message Notification

Once the private channel is created, we can send message notifications to users within that channel. Here's the Python code to send a message notification:

def send_message_notification(self, send_user, receiver_users, message): 

        """ 

        This method is used for sending message notifications to specific users. 

        """ 

        for rec in receiver_users: 

            channel = self.env['discuss.channel'].search([ 

                ('name', '=', send_user.name +','+ rec.name), 

                ('channel_type', '=', 'chat')], limit=1) 

            if not channel: 

                channel = self.env['discuss.channel'].search([ 

                    ('name', '=', rec.name +','+ send_user.name), 

                    ('channel_type', '=', 'chat')], limit=1) 

            if channel: 

                channel.message_post(body=message) 

            else: 

                channel_id = self.create_private_channel(send_user, rec) 

                channel_id.message_post(body=message) 

Here For Example When Sale Order is confirmed, and Team Leader of Sale Team received notification.

from odoo import models 

class SaleOrder(models.Model): 

    """ A 'discuss.channel' is a discussion group that may behave like a listener 

    on documents. """ 
    _inherit = 'sale.order' 

    def action_confirm(self): 

        result = super(SaleOrder, self).action_confirm() 

        approver = self.team_id.user_id 

        message_content = "Hello %s, I have confirm the sale order %s." % ( 

            approver.name, self.name) 

        mail_channel = self.env['discuss.channel'] 

        mail_channel.send_message_notification(self.env.user, 

                                               approver, 

                                               message_content) 

        return result 

Here attached screenshot show Mitchell Admin confirm the order and Team leader Jack received the notification.

Conclusion

Display notifications play a vital role in keeping users informed about important updates or messages in a business management software like Odoo. By implementing the code snippets provided in this blog post, you can enhance the communication experience within your Odoo application, thereby improving overall efficiency and productivity.

As an official Odoo Silver Partners, we offer comprehensive Odoo development services tailored to guide you through consultation, implementation, and ongoing support. Feel free to reach out to us with any questions or for further information – we're here to assist you.

Contact us

For Your Business Requirements

Text to Identify Refresh CAPTCHA
Background Image Close Button

2 - 4 October 2024

Hall: 10, Booth: #B8 Brussels, Belgium