How to Create Server Actions in Odoo14 .

Posted By : Aakash Vishwakarma | 30-Dec-2022

How to create Server Actions in odoo14

In Odoo server action is a form of automation. The server action provides a simple way to execute an action manually, with a button click.

In Odoo, enable developer mode and then go to settings options. In side technical, we can find THE server action option where we can create a server action or we can also create a server action with the help of coding.

Let's write a simple code to create a server action.

 

1. The first step is to create an XML file and write the below code.

 

<record model="ir.actions.server" id="uniq_id">
            <field name="name">Server Name</field>
            <field name="model_id" ref="model_model_name"/>
            <field name="state">code</field>
            <field name="code">
                       model.function_name()
            </field>
</record>

 

This will create a server action and when we hite this action it will cal a function or executive the line of code which we have writen in code section.

Lets write a code to create some records with the help of server action.

 

<record model="ir.actions.server" id="create_product_master">
            <field name="name">Create Product Master Automatically</field>
            <field name="model_id" ref="model_server_actions"/>
            <field name="state">code</field>
            <field name="code">
                     model.create_product_master()
            </field>
</record>


<menuitem id="create_product_master_action" name="Create Product Master Automatically" action="create_product_master"   parent="procurement_config" groups="base.group_user"/>

<menuitem id="procurement_config" name="Procurement"  parent="sale.sale_menu_root"  groups="base.group_user"/>


 

This will create a server action and create a button in the side procurement menu on the sale module.

When we hit this button it will call a function name create_product_master.

Let's create a function with the name create_product_master

 

from odoo import fields, models, api
import datetime
from datetime import timedelta
from pytz import timezone


 


class ServerAction(models.Model):
       _name = "server.actions"
       _description = "Here We define all server action functions"


       def create_product_master(self, **params):
              records = self.env['product.product'].sudo().search([('type', '=', 'product'), ('product_template_attribute_value_ids.product_attribute_value_id.name', 'in', ['A'])])
              for rec in records:
                    result = self.env['product.master'].sudo().search([('product_id', '=', rec.id)])
                    if not result:
                        packet = []
                        packets = self.env['product.pack'].sudo().search([('uom_id', '=', rec.uom_id.id)])
                        for pack in packets:
                              packet.append(pack.id)
                        vals = {
                               'product_id': rec.id,
                               'pack_ids': [(6, 0, packet)]
                         }
                        self.env['product.master'].sudo().create(vals)
                    else:
                          p1 = []
                          p2 = []
                          for pack in result.pack_ids:
                                p1.append(pack.id)
                          packets = self.env['product.pack'].sudo().search([('uom_id', '=', rec.uom_id.id)])
                          for pack in packets:
                                p2.append(pack.id)
                          packet = [x for x in p2 if x not in p1]
                          for pp in packet:
                                result.sudo().write({
                                'pack_ids': [(4, pp)]
                                })





 



 



 


 

Related Tags

About Author

Author Image
Aakash Vishwakarma

Akash is working as an Odoo developer with an overall 1+ years of experience. His areas of expertise are Python, Odoo, and MySQL. He is honest about his work.

Request for Proposal

Name is required

Comment is required

Sending message..