Display Notifications in Odoo: Using ir.actions.client for Alerts

Abid Patel
30-Oct-2024 Updated : 30-Oct-2024

Learn how to use ir.actions.client to display notifications in Odoo. This method enables custom alerts and feedback for users directly in the Odoo interface.

Display Notifications in Odoo: Using ir.actions.client for Alerts

In Odoo notifications may appear with ir.actions.client. These provide prompt user feedback without page refreshes. They also avoid redirects. This approach proves especially helpful for affirming actions. It also can be used for showing warnings or communicating errors directly to users in their current view.

What is ir.actions.client used for?

Notifications of course. This action type is found in Odoo. Notifications may be displayed with custom messages and settings. This notification can be configured to show as a non-intrusive popup. It can also be set as either temporary or sticky.

An example shows the use of ir.actions.client for notification display. A code snippet details use of ir.actions.client for notification display. In this example a notification is shown. It's an informational message. It appears after sending an invitation. This invitation is for two-factor authentication (2FA).

python

from odoo import models, api, _

class ResUsers(models.Model):
    _inherit = 'res.users'

    def action_invite_for_2fa(self):
        users_to_invite = self.filtered(lambda u: not u.otp_enabled)
        # Logic for sending invitation...
        return {
            'type': 'ir.actions.client',
            'tag': 'display_notification',
            'params': {
                'type': 'info',
                'sticky': False,
                'message': _("Invitation to use two-factor authentication sent for the following user(s): %s" %
                             ', '.join(users_to_invite.mapped('name'))),
            }
        }

Explanation of the Code:

  • ▹ Method Definition: Here in this example action_invite_for_2fa is a method. It invites users. Users are invited for 2FA.
  • ▹ Users Filter: The line users_to_invite = self.filtered(lambda u: not u.otp_enabled) filters. It filters out users. These users have not enabled 2FA yet.
  • ▹ Return Notification Action:
  • ▹ Type: 'type': 'ir.actions.client' specifies the action type.
  • ▹ Tag: 'tag': 'display_notification' sets the tag to show a notification.
  • ▹ Params: Inside params, settings are configured.
  • ▹ Type: It determines message type ('info', 'warning', 'danger').
  • ▹ Sticky: Set to True, it makes the notification visible until manually dismissed. False makes it disappear automatically.
  • ▹ Message: A custom message. Here success message displays. It shows the names of invited users.

Advanced Customization.

Tweaking of the notification is possible. This is done by changing its parameters.

Warning Message:

python

'type': 'warning',
'sticky': True,
'message': _("This action requires administrator privileges.")

Error Message:

python

'type': 'danger',
'message': _("An error occurred. Please try again.")

The Benefits of ir.actions.client for Notifications

  • ▹ Instant Feedback. Users get fast updates without page reloads.
  • ▹ Customizable Duration. Notifications can be sticky. They remain until dismissed.
  • ▹ Numerous Types. Different kinds of notifications help convey urgency. They include info, warning and danger.

Conclusion

Utilization of ir.actions.client in Odoo is both elementary and potent. It boosts user interaction significantly. It does this by offering immediate feedback. Using a few code lines, one can inform users of their actions. They can also caution them about problems or spotlight errors. All this can be done right from inside the form or list view. This makes for a regular and quick user experience. It's also responsive. Usage of ir.actions.client in Odoo supplies elementary potent means for enhancing interaction. Here it provides instant feedback. Inform users about actions implement caution on issues. Highlight errors. All carried out within the form or list view, making user experience seamless and responsive. All these actions can be completed using few lines of code.

Make a Comment

Your email address will not be published. Required fields are marked *