Home Blog How To Create An Action Button On A List View
How To Create An Action Button On A List View

How To Create An Action Button On A List View

We can change the state of one or many records in the list view with just 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 the mail module because we want to change the state of all emails from the delivery failed state to the 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.

Inheriting The Default Mail

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

Inheriting The Default Mail

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.

Inheriting The Default Mail

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

Inheriting The Default Mail

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.

Inheriting The Default Mail

Now create a wizard.py in a custom module.

Inheriting The Default Mail

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.

Inheriting The Default Mail

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 create an xml file with a suitable name & mention it inside the __manifest__.py file of the custom module like this :

Inheriting The Default Mail

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

The functionality of This Custom Module

Step 1: We go to the Emails menu.

The functionality of This Custom Module

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

The functionality of This Custom Module

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.

The functionality of This Custom Module

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

The functionality of This Custom Module

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

The functionality of This Custom Module

We Hope This blog is helpful to you, and if you have any queries related to the same, feel free to ask in the comment section.

 Subscribe our Newsletter for Odoo tips, technical insights, and more!

Kanak Infosystems is a group of professionals who help you with Odoo Services like Development, Customization, Installation, Implementation, and 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 at sales@kanakinfosystems.com OR call us at +91-9818108884

Leave a Comment

Your email address will not be published.

Submit
Your comment is under review by our moderation team.