How to create and configure server actions in Odoo18
Typically, they are activated by button clicks and automated workflows. Server actions in Odoo allow you to perform server-side automation. They augment your Odoo system’s capabilities by allowing the execution of complex business logic, and they are written in Python. Normally, they are activated by button clicks and automated workflows. They make your Odoo system more functional by supporting the execution of complicated business logic and are written in Python.
First, enable developer mode/ debugging in Odoo to see all possible server actions in odoo18. Once this feature is enabled go to Technical then select Server Actions in the Settings.
Here you can browse a list of every server action configured within your environment.
What Can Odoo 18’s Server Actions Do?
Server actions are very versatile and can automate quite a lot of jobs. Some of the key skills include:
- Execute Python Code: Run Python scripts to apply custom logic automatically.
- Generate New Entries: Automatically create entries in any Odoo model.
- Modify Existing Entries: Change entries to ensure the correctness of the information by changing records according to some preset rules.
- Send Email and SMS Messages: Report the happening of certain events to relevant parties.
- Managing followers: Followers can be assigned to important records.
- Schedule next activities: Workflow can be enhanced by auto-creation of follow-up activities
- Chain multiple actions: Multiple server actions are strung together to form a workflow with complex steps.
For Example: Can be used to authorize or approve multiple orders simultaneously.
Let’s create server actions in odoo18 that authorize orders from the “draft” state to the “authorize” state.
1. Define the Server Action in XML
Navigate to the data directory of your Odoo 18 custom module to create this server action. Define the action by opening the file data.xml as follows:
Explanation:
- name: The display name of the server action in the menu of the server actions.
- model_id: This is what specifies that it is linking to which model, in this case, the purchase. requisition.
- binding_model_id: It is displayed only if you are working with a purchase. requisition and it binds the action to the model.
- The action is visible according to the binding_view_types; “list” means that the action can be activated from the list view.
*state: Identifies the type of action, “code” here meaning run Python script.
- code: The Python script that gets to run when invoked by the action.
2. Implement the Python Function
Now we need to define the Python function that will be called by the server action. Add the following code in the Python file for your res. partner model:
Explanation:
- bulk_order_approval_authorize: This function checks whether the purchase requisition record is in a draft state. If so, the function moves the record from draft to authorised state.
The newly created server action can be seen in the server actions menu in settings under the technical section.
3. Execute the Server Action
Now that you have created the server action, you will be able to use it from the purchase requisition list view. Here’s how to test it:
- Go to the Purchase module.
- Select several records that are in draft state.
- Under the Actions menu, you should see the newly created server action named “Authorize Requisitions.”
- By Clicking the server action odoo will change the state of that record to the ‘authorize’ state.
With this server action in place, you can automate data-handling operations in Odoo, such as authorizing bulk orders, updating date for orders, field synchronization etc. This small automation saves time on repetitive actions and helps ensure the data is consistent.
Server actions in Odoo 18 come with so many chances for optimizing workflow and automating processes. You can increase efficiency in your ERP system dramatically and also reduce more manual intervention if you explore and modify the server actions.
"Unlock the Full Potential of Your Business with Odoo ERP!"
"Get a Cost Estimate for Your ERP Project, Absolutely FREE!"
Get a Free Quote