How to popup success message in odoo?

Viewed 6972

I am sending invitation by clicking button after clicking button and successfully sending invitation there is pop up message of successfully invitation send. But the problem is that the main heading of pop up message is Odoo Server Error. That is because I am using

raise osv.except_osv("Success", "Invitation is successfully sent")

Is there any alternative to make it better.

3 Answers

When I need something like this I have a dummy wizard with message field, and have a simple form view that show the value of that field.

When ever I want to show a message after clicking on a button I do this:

     @api.multi
     def action_of_button(self):
        # do what ever login like in your case send an invitation
        ...
        ...
        # don't forget to add translation support to your message _()
        message_id = self.env['message.wizard'].create({'message': _("Invitation is successfully sent")})
        return {
            'name': _('Successfull'),
            'type': 'ir.actions.act_window',
            'view_mode': 'form',
            'res_model': 'message.wizard',
            # pass the id
            'res_id': message_id.id,
            'target': 'new'
        }

The form view of message wizard is as simple as this:

<record id="message_wizard_form" model="ir.ui.view">
    <field name="name">message.wizard.form</field>
    <field name="model">message.wizard</field>
    <field name="arch" type="xml">
        <form >
            <p class="text-center">
                <field name="message"/>
            </p>
        <footer>
            <button name="action_ok" string="Ok" type="object" default_focus="1" class="oe_highlight"/> 
        </footer>
        <form>
    </field>
</record>

Wizard is just simple is this:

class MessageWizard(model.TransientModel):
    _name = 'message.wizard'

    message = fields.Text('Message', required=True)

    @api.multi
    def action_ok(self):
        """ close wizard"""
        return {'type': 'ir.actions.act_window_close'}

Note: Never use exceptions to show Info message because everything run inside a big transaction when you click on button and if there is any exception raised a Odoo will do rollback in the database, and you will lose your data if you don't commit your job first manually before that, witch is not recommended too in Odoo

If previous answer is not working, try this:

In odoo 15 version, you can use this:

# show success message
title = _("Successfully!")
message = _("Your Action Run Successfully!")
return {
    'type': 'ir.actions.client',
    'tag': 'display_notification',
    'params': {
        'title': title,
        'message': message,
        'sticky': False,
    }
}

Note:

Remember to add this line in your python file to translate the message:

from odoo import _

Also, you will need to add action = ... in xml file or else it won't work:

<field name="code">action = model.your_function()</field>

Example xml code:

<?xml version="1.0" encoding="utf-8"?>
<odoo>
    <record id="action_your_action_name" model="ir.actions.server">
        <field name="name">Your Action</field>
        <field name="model_id" ref="model_your_model_name"/>
        <field name="binding_model_id" ref="mymodule.model_your_model_name"/>
        <!-- set this action is appear in form or list -->
        <field name="binding_view_types">list</field>
        <field name="state">code</field>
        <!-- function called -->
        <field name="code">action = model.your_function()</field>
    </record>
</odoo>

i'm not sure why, but if we remove the action = ... part, so it look like this, it won't work:

<field name="code">model.your_function()</field>

Hope it's help, thanks.

if you have a button and the text is static, you can also use a button confirm attribute in xml,ex.

 <button string="Change Next Activity Date" name="change_next_activity_date"
                                type="object" class="btn-primary" confirm="are you sure you want to change the Date?"/>
                        

It will display a confirmation dialog with yes and Cancel button on it and won't break the process. Same as Yes or no option.

Related