+91 9818211679, +91 9998040067       [email protected]

How To Create An Action Button On A List View

We can change the state of one or many records in list view just in one click on the action button.

We can add this action button in any module when we want to perform a single click action in a list view.

In This Scenario, we add the Cancel Emails button in Action through the Wizard in mail module because we want to change the state of all emails from delivery failed state to cancel state.

Inheriting The Default Mail

First of all we need to create an __init_.py & __manifest__.py file’s for our Custom Module. These files are necessary for calling the py files from folders & declaring the module information. We can create these files like this.

The given file imports the models folder of the module. This file needs to be placed inside our module.

Odoo CMS - a big picture

The given file imports the mail.py file. This file needs to be placed inside the models folder of the custom module.

Odoo CMS - a big picture

The given file is the __manifest__.py file. This file is a declaration of the contents of the module & its naming & is also used for calling the view files. This file needs to be placed inside the custom module.

Odoo CMS - a big picture

Now create a mail.py & inherit the “mail.mail” model of the mail module inside the file.

Odoo CMS - a big picture

In this code, class MailMail(models.Model): MailMail is a class name & models.Model (model is a class that maps to the data relation (table) and potentially bridge tables (e.g. for many to many relations) is a default code in odoo.

 _inherit = [‘mail.mail’]  -  when you inherit any model use this code _inherit = ‘model.name’  & “mail.mail” is a model name which is      inherited.

After that, we need to create a wizard of our custom module to add a button (Cancel Emails) in Action.

First of all, we need to create a folder (wizard) in our module.

After that, we need to create an __init_.py file for our wizard.

Odoo CMS - a big picture

Now create a wizard.py in a custom module.

Odoo CMS - a big picture

In this py code, class CancelWizard(models.TransientModel): CancelWizard is a class name & (models.TransientModel) .

TransientModel is a class that stores data in the database temporary and it is automatically cleaned up on a defined period of time.

        _name = ‘cancel.wizard’ is a model name of this class

Then we create a button: - cancel_emails is a button name.

      moves = self.env[‘mail.mail].browse(context.get{‘active_ids’)) - context or moves is a variable, When If you are in a tree view context.get('active_ids') returns a list of checked elements ids in inherited model.

       move_to_post = self.env[‘mail.mail’] - move_to_post is a variable name & self.env to create an environment for “mail.mail” model.

Then we create a “for loop” - for move in moves: – move is a variable, which is used to check the condition in moves.

     If move.state != ‘cancel’:  in this code move check the state of moves != cancel state then move_to_post += move this code change the state of every record.

    move_to_post.cancel() - cancel is a button name which is defined in inherited model and all records are sent to cancel state through this button.

If we want to change another state, then we need to call that method in place of cancel.

    return {'type': 'ir.actions.act_window_close'} it is a defult method in odoo.

After that, we need to create a view file of our py file where we need to add the custom button in Action on Emails page.

It is necessary to create the action for this button in the view file.

The main points which need to be kept in mind while creating this action. These involve properly providing the inherit id of the view file.

Odoo CMS - a big picture

In this xml code,

   <button name=”cancel_emails”- It is a button name, which is defined in py file & string=”Ok”-  Ok is  the button name in odoo & class=”btn-primary”- Ok is a primary button to perform an action.

      <button string=”Cancel”- Cancel is a secondary button.

Then we create an action:-

      <field name=”name”>Cancel Emails</field> Cancel Emails is a string of button name to show in odoo.

     <field name=”res_model”>cancel.wizard</field> cancel.wizard is a wizard model name, which you are creating.

      <field name=”view_id” ref=”email_cancel_view”/> emai_cancel_view is a form view id, which you are creating.

After that, we must proceed to creating an xml file with a suitable name & mention it inside the __manifest__.py file of the custom module like this :

Odoo CMS - a big picture

After doing this, let’s move to the next part, we go to the terminal and run the server .

Functionality Of This Custom Module

Step 1: We go to the Emails menu.

Odoo CMS - a big picture

Step 2 : Select the Emails which are present in the delivery failed state.

Odoo CMS - a big picture

Step 3: We go to the Action and tap to the action button, it will be automatically open a drop-down menu.

Step 4: Tap to the Cancel Emails button.

Odoo CMS - a big picture

Step 5: It will be automatically open a popup dialog box and select the Ok button.

Odoo CMS - a big picture

Step 6: And finally all selected Emails are automatically gone to Cancel state.

Odoo CMS - a big picture

We Hope This blog helpful to you and you have any query related the same feel free to ask in the comment section. Kanakinfosystems is a group of professionals who help you with Odoo Services like Development, Customization, Installation, Implementation, Odoo modules Development & also provide Best ERPNext Services to our global clients with 100% support. Connect with us on social media for more updates.

Feel free to Contact Us if you want any Odoo & ERPNext Services for your organization. You can also email us @[email protected] OR call us at +91-9818211679, 011-42143250