Enable Dark Mode!
how-to-create-and-configure-server-actions-in-the-odoo-17.jpg
By: Vishnuraj P

How to Create & Configure Server Actions in the Odoo 17

Technical Odoo 17

Server Actions in Odoo serve as a dynamic mechanism for automating processes without the need for extensive custom development. These actions enable users to define a sequence of operations or tasks that the system executes automatically under specific conditions. From creating records and updating fields to triggering complex business logic, Server Actions empower users to automate routine activities and respond intelligently to various scenarios within the ERP environment.

To view all the server actions available in the Odoo system, enable debug mode, then go to Settings -> Technical -> Server Actions.

How to Create & Configure Server Actions in the Odoo 17-cybrosys

This is the list of existing server actions in Odoo 17.

How to Create & Configure Server Actions in the Odoo 17-cybrosys

A server action can perform the following types of actions.

1. Execute a python code

2. Create new record

3. Update new record

4. Sent an email

5. Sent SMS message

6. Add followers

7. Create next activity

8. Execute multiple actions

Create a new server action

In this blog create a server action for copying data from the 'phone' field to the 'mobile' field in the 'res.partners' model.

The server action should be defined in the “ir_action_data.xml” file inside the “data” directory of your module.

<record id="copy_phone_to_mobile" model="ir.actions.server">
   <field name="name">Copy phone to mobile</field>
   <field name="model_id" ref="base.model_res_partner"/>
   <field name="binding_model_id" ref="base.model_res_partner"/>
   <field name="binding_view_types">list</field>
   <field name="state">code</field>
   <field name="code">
       action = records.action_copy_phone_to_mobile()
   </field>
</record>

In this code 

- name: specifies the name of the server action, which shows on the server actions menu.

- model_id: is the model linked to the server action, it should be in this form <module>.model_<model_name>

- binding_model_id:  It specifies that the action is bound to the referencing model.

- binding_view_types: specifies the action is intended to be available in which views of the associated model.

- state: It specifies the type of server action we discussed above, in our case, we choose it as “code” for executing a Python script.

- code: specify the Python script to be executed.

After adding the XML code, you should include the function “action_copy_phone_to_mobile()” in the res.partner model, as mentioned in the server action.

def action_copy_phone_to_mobile(self):
   for record in self:
       if record.phone and not record.mobile:
           record.write({'mobile': record.phone})

Now, we can use this server action on the contacts list view.

Select some contacts that do not have mobile fields and have phone fields, then under the “Actions” menu, there will be a newly created server action “Copy phone to mobile” button.

How to Create & Configure Server Actions in the Odoo 17-cybrosys

The result would be.

How to Create & Configure Server Actions in the Odoo 17-cybrosys

The newly created server action is also visible in Server actions in the Technical menu from settings.

How to Create & Configure Server Actions in the Odoo 17-cybrosys

In summary, Odoo Server Actions empower users to automate diverse processes, enhancing efficiency and reducing manual intervention, thereby optimizing operations within the ERP environment.


If you need any assistance in odoo, we are online, please chat with us.



0
Comments



Leave a comment



whatsapp
location

Calicut

Cybrosys Technologies Pvt. Ltd.
Neospace, Kinfra Techno Park
Kakkancherry, Calicut
Kerala, India - 673635

location

Kochi

Cybrosys Technologies Pvt. Ltd.
1st Floor, Thapasya Building,
Infopark, Kakkanad,
Kochi, India - 682030.

location

Bangalore

Cybrosys Techno Solutions
The Estate, 8th Floor,
Dickenson Road,
Bangalore, India - 560042

Send Us A Message