Development Book V17:Python code server actions

The most adaptable type of actions that assist users in using Python code to fulfill their needs are those provided by code server actions. Its ability to integrate sophisticated logic into Odoo directly from the front end makes it beneficial. Let's see how the Python code server activities are put into practice.

After enabling the developer mode go to Settings > Technical > Automation > Automated Rules. Then create a new action.

For example, Let’s see how a warning is set on the record during creation.


id not record.email:
raise UserError("Please add mail.")
odoo-development

This is a basic example to show the python code server actions. The fields that are used here;

Model: model upon which this action needs to be executed.

Action to do: type of action to be performed.

Trigger: triggering conditions like on update, create, deletion, etc.

Apply on: condition specified before executing the action

Python Code: python code is explained here.

odoo-development

After saving the action, it will be necessary to add an email while creating any contact. Otherwise, it will show UserError according to the python code server action.

Automated actions on time conditions

Time circumstances can be used to initiate automated activities. Therefore, the automated actions can carry out any task that needs to be completed at a certain time.

Let's examine how Odoo does this.

’Navigate to Settings > Technical > Automation > Automated Actions after turning on developer mode. Let's then make a fresh record for that.

odoo-development

Model: Model on which the action needs to be performed.

Action To do: Select the type of action.

Trigger: Select the triggering type. Here it will be “Based on Time condition”

Email Template: email template to be used. It needs to be selected if the type is “send Email”

Trigger Date: it decides when the action should be triggered.

Delay : it can have negative values so that the action will be triggered before the trigger date.

Automated actions on event conditions

Automated actions can be set to trigger the action based on a specific event. Let’s check if its working by using an example.

Assume that the user's access permissions to the project manager and project leader are necessary for them to be able to update the project status. Let's now build a server action to display user error that is based on Python code. It verifies whether the project manager and the logged-in user are the same. An error message will be raised if it fails.

odoo-development

To carry out this action while it is being updated, let's now build another action. This is how it will look:

odoo-development

The fields are described here as follows:

Model: The necessary model that will serve as the basis for the action.

Trigger: The condition that sets off the action.

Fields that trigger an action: If any fields are specified, an update to these fields will be the basis for action.

Prior to updating the domain: Any requirements listed here will be met prior to the record being updated.

Apply on: Prior to the action rule, any criteria that are provided on the field will be satisfied.

What to do: Here, the kind of activity is described.

If the user is not the project manager for that specific project, an error will be raised when they try to alter the record after these requirements have been added.

odoo-development
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