(#75) How to Create & Configure Server Actions in Odoo 18
Arsalan Yasin
Odoo Functional Consultant | Simplifying ERP Chaos for Global SMEs | 10+ Years of Scaling Systems That Last
Server actions in Odoo are Python-based automation tools that allow you to execute tasks on the server side. They can be triggered by events like button clicks or automated workflows, making it possible to implement complex business logic and enhance your Odoo system's functionality.
To access all server actions, first enable developer (debug) mode. Then, go to the Settings menu, navigate to the Technical section, and select Server Actions.
Here, you can explore a list of all the server actions configured in your environment.
What Can Server Actions Do in Odoo 18?
Server actions in Odoo 18 are powerful automation tools that streamline operations by executing tasks based on specific triggers. Here are some of their key capabilities:
Practical Example: Copying Data Between Fields in res.partner
Here’s a simple example of a server action: copying the value from the phone field to the mobile field in the res.partner model. This can be done using a Python script within the server action.
1. Define the Server Action in XML
To create this server action, navigate to the "data" directory in your Odoo 18 custom module. Open the ir_action_data.xml file and define the action with the following XML code:
<?xml version="1.0" encoding="UTF-8" ?>
<odoo>
? ? <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>
</odoo>
Explanation:
* name: A descriptive name for the server action, as displayed in the server actions menu.
* model_id: Specifies the model associated with the action (in this case, res.partner).
* binding_model_id: Binds the action to the model, so it is only available when working with res.partner.
* binding_view_types: Defines where the action is accessible, with "list" indicating it will be available in the list view.
* state: This specifies the type of action, where "code" means it will execute a Python script.
领英推荐
* code: Contains the Python script that will be executed when the action is triggered.
2. Implement the Python Function
Next, define the Python function that the server action will call. Add the following code to your res.partner model's Python file:
from odoo import models
class ResPartner(models.Model):
? ? _inherit = 'res.partner'
? ? def action_copy_phone_to_mobile(self):
? ? ? ? for record in self:
? ? ? ? ? ? if record.phone and not record.mobile:
? ? ? ? ? ? ? ? record.write({'mobile': record.phone})
Explanation:
* action_copy_phone_to_mobile: This function checks if a res.partner record has a phone number but no mobile number. If so, it copies the phone number to the mobile field.
Once you've reviewed the server actions, you'll see that the newly created server action is now available.
3. Execute the Server Action
Once you've created the server action, it will be available for use in the contacts list view. To test it, follow these steps:
1. Go to the Contacts module.
2. Select several contacts that have data in the "phone" field but not in the "mobile" field.
3. Under the Actions menu, you should see the newly created server action named "Copy Phone to Mobile."
4. Click the action, and Odoo will copy the phone numbers into the corresponding mobile fields.
By using this server action, you can automate data management tasks in Odoo, such as keeping fields synchronized. This simple yet effective automation ensures data consistency and reduces the time spent on repetitive tasks.
Server actions open up limitless opportunities for automating processes and streamlining workflows in Odoo 18. By leveraging and customizing them, you can greatly improve your ERP system’s efficiency while minimizing manual effort.