There may be times when we need to carry out complex processes; in these cases, we can run Python code. There are some restrictions when using other Action To Do options.There are no constraints when writing Python code.
So, in this documentation, we will use Python to create a new automation to confirm the sale order if the total is less than 500. To do so, first launch Studio and then select Automations. Click the Create button to start a new automation.
Fill out all of the required fields, then click the “Add an action” button and select Execute Code.Now that we've seen how to use Python code space, we can write Python code here.
In this example, we've added an if statement and called the action_confirm button function. The following variables are accessible in this section of Python code:
- env: The Odoo Environment in which the action is performed.
- Odoo is a model. The record on which the action is triggered is represented by a void recordset.
- record: the record on which the action is triggered; may be null.
- records: a recordset of all records on which the action in multi-mode is triggered; may be void.
- Python libraries for time, datetime, dateutil, and timezone
- float_compare: An Odoo function for comparing floats based on precision.
- log: log(message, level='info'): logging function to record debug information in ir.logging table
- _logger: _logger.info(message): logger that emits messages in server logs.
- UserError: Warning Exception to use with raise
- Command: x2Many command namespace
- To return an action, assign: action = {...}
We also added a domain to filter the records. In this manner, we can include Python code in an automated action.