自动化规则¶
自动化规则允许针对特定触发执行一个或多个预定义操作,例如,当某个字段设置为特定值时创建活动,或在最后一次更新 7 天后归档记录。
When creating an automation rule, it is possible to add conditions that must be met for the automation rule to run, e.g., the opportunity must be assigned to a specific salesperson, or the state of the record must not be Draft.
To create an automation rule with Odoo Studio:
开启 Studio 并点击 自动化,然后点击 新建。
给自动化规则起一个清晰、有意义的名字,以明确其目的。
Select the Trigger and, if necessary, fill in the fields that appear on the screen based on the chosen trigger.
Click Add an action in the Actions To Do tab.
小技巧
If no explicit name is entered, the name of the action will be automatically generated based on the action you define; the name can be updated at any time.
Select the Type of action and complete the relevant fields based on the chosen action.
Click Save & Close or, to define additional actions, Save & New.
Example
为确保跟进满意度较低的客户,该自动化规则会在销售订单创建 3 个月后为满意度低于 30% 的客户创建活动。

触发器¶
触发器 用于定义自动化规则运行所需的活动类型。可用的触发器取决于 模型。共有五个触发器类别:
添加条件¶
域筛选器允许您确定自动化规则应针对或排除的记录。高效筛选可避免对不受规则影响的记录进行不必要的处理,从而提高整体性能。
小技巧
在创建自动化规则前激活开发者模式,以便最灵活地添加域筛选器。
根据所选触发器的不同,可以定义一个或多个记录在触发*前*和/或触发*后*必须满足的条件。
域名更新前 定义了记录在触发事件发生*之前*必须满足的条件,例如,记录必须具有
类型 = 客户发票
和状态 = 已发布
。With developer mode activated, click Edit Domain, if available, then New Rule.
The Apply on domain defines the conditions a record must meet after the trigger event occurs, e.g., the customer invoice must have
Payment Status = Partially Paid
.With developer mode activated, click Edit Domain then New Rule.
小技巧
If developer mode is not activated, to define an Apply on domain, click Add condition. Complete or delete the suggested condition. To add a condition, click New Rule.
当发生 触发条件。
Example
如果是个人而非公司的现有联系人首次设置电子邮件地址(与修改电子邮件地址相反)时应执行自动操作,则使用 电子邮件未设置`和 `公司未设置`作为 :guilabel:`域名更新前
,并使用 电子邮件已设置`作为 :guilabel:`应用
域。

注解
The Before Update Domain is not checked upon the creation of a record.
值更新¶
当数据库中发生特定变化时触发自动操作。这类触发器取决于模型,并基于常见的更改,如添加特定标记(如添加到任务中)或设置字段值(如设置 用户 字段)。
选择触发器,然后根据需要选择数值。
电子邮件活动¶
接收或发送电子邮件时触发自动操作。
时间条件¶
在与日期字段或记录的创建或更新相关的时间点触发自动操作。可使用以下触发器:
基于日期字段:在所选日期字段的日期之前或之后的一段时间内触发操作。
创建后:在创建并保存记录后的一段时间内触发该操作。
上次更新后:在编辑并保存现有记录一段时间后触发该操作。
然后,您可以定义:
a Delay: Specify the number of Minutes, Hours, Days, or Months after which the action should be triggered. If you selected the Based on date field trigger, the action can be triggered After or Before the selected date field.
注解
默认情况下,调度程序每 240 分钟或 4 小时检查一次时间触发的自动化规则。对于订单日期后 3 个月或上次更新后 7 天等延迟情况,这个频率一般就足够了。
对于少于相当于 2400 分钟或 40 小时的延迟,系统会重新计算该检查的频率,以确保尽可能遵守更细化的延迟,如事件开始日期和时间前 1 小时或创建后 30 分钟。
To view or manually edit the frequency of the scheduler for a time-triggered automation rule, with developer mode activated, click Scheduled action.
In the Automation Rules: check and execute scheduled action that opens, update the value of the Execute Every field, if desired. Clicking Run Manually triggers the scheduled action to run immediately. To return to the automation rule setup, click the automation rule name in the breadcrumbs.
Extra Conditions: Click Add condition, then specify the conditions to be met for the automation rule to run. Click New Rule to add another condition.
The action is executed when the delay is reached and the conditions are met.
自定义¶
触发自动操作:
On create: when a record is saved for the first time.
On create and edit: when a record is saved for the first time and any subsequent time.
On deletion: when a record is deleted.
On UI change: when a field’s value is changed on the Form view, even before the record is saved.
For the On create and edit and On UI change triggers, you must then select the field(s) to be used to trigger the automation rule in the When updating field.
警告
如果在 :guilabel:`更新时`字段中没有选择任何字段,该自动化操作可能会对同一条记录执行多次。
(可选)您还可以在 :guilabel:`应用于`字段中定义额外的触发条件。
外部¶
使用 网络钩子,根据外部系统或应用程序中的特定事件触发自动操作。
在 Odoo 中配置网络钩子(生成网络钩子 URL 并定义目标记录)后,需要在外部系统中实施。
警告
*强烈建议*在决定使用网络钩子和整个实施过程中咨询开发人员、解决方案架构师或其他技术人员。如果配置不当,网络钩子可能会扰乱 Odoo 数据库,并可能需要一段时间才能恢复。

注解
还可以设置一个自动操作,当 Odoo 数据库中发生事件时,该操作 向外部系统的网络钩子 发送数据。
操作¶
Once you have defined the automation rule’s trigger, click Add an action in the Actions To Do tab to define the action(s) to be executed.
小技巧
You can define multiple actions for the same automation rule. By default, actions are executed in the order in which they were defined.
This means, for example, that if you define an Update record action and then a Send email action where the email references the field that was updated, the email uses the updated values. However, if the Send email action is defined before the Update record action, the email uses the values set before the record is updated.
To change the order of defined actions, click the (drag handle) icon beside an action and drag it to the desired position.
更新记录¶
This action is used to update one of the record’s (related) fields. The following options are available:
Update: updates the selected field with the specified value.
Update with AI: updates the selected field dynamically based on the AI prompt provided. This option requires the Odoo AI app to be installed.
Sequence: update the selected field with a defined sequence.
Compute: updates the selected field dynamically using Python code.
To define the action:
With the Update, Update with AI, Sequence or Compute option selected, as relevant, select or search for the field to be updated or computed. If needed, click the (right arrow) next to the field name to access the list of related fields.
Provide the relevant information based on the option selected.
Update¶
Select or enter the updated value for the field.
If a many2many field is being updated, choose whether the field must be updated by Adding, Removing, or Setting it to the selected value or by Clearing it.
Example
If you want the automated action to remove a tag from the customer record, set the Update field to Customer > Tags, select by Removing, then select the tag to be removed.

Update with AI¶
Enter a prompt to instruct Odoo AI how to update the field. Type /
to open the AI
prompt tools; use Field Selector to tell Odoo AI which related fields to check for
context, and Records Selector to provide possible values for the updated field.
Example
If you want the automation rule to update the Assignees field of a newly created task based on the employees’ expertise, you can write a prompt to instruct Odoo AI to check the Display Name field of the task for the context, then to assign the most suitable employee.

Sequence¶
Select an existing sequence or, to create a new sequence:
Click Search more, then click New.
In the window that opens, enter the Name of the sequence.
Configure the sequence, adding a Prefix and/or Suffix as desired.
小技巧
Use dynamic placeholders like Current Year with Century: %(year)s in the Prefix and/or Suffix to create sequences with elements like current year, month, etc. Possible placeholders are shows at the bottom of the window.
Click Save.
Example
If you want the automated action to create a sequential customer reference each time a new customer is created, set the Sequence field to Reference, then, in the dropdown, click Search more. Click New to create a new sequence.
In the example, each new customer receives a sequential reference with the prefix `

Compute¶
Enter the code to be used for computing the field’s value.
Example
If you want the automation rule to compute a custom datetime field, Escalated on, when a task’s priority is set
to Very high
(three stars), you can define the trigger Priority is set to to Very
High
and define the Update Record action as follows:

Create Record and Duplicate Record¶
These actions are used to create a new or duplicated record on any model.
To define the action:
With Create Record or Duplicate Record selected as the Type of action, as relevant, select the required model in the Record to Create field; the field contains the current model by default.
Specify a Name for the record, or, if the action duplicates a record, indicate the record being duplicated in the Duplicate from field.
If the new or duplicated record is being created on another model, select a field in the Link Field field to link the record that triggered the creation of the new or duplicated record.
小技巧
The Link Field dropdown menu only contains one2many fields existing on the current model that are linked to a many2one field on the target model.
Example
If you want the automation rule to duplicate a project, e.g. a project template with predefined
tasks, when an opportunity is set to Won
, add a custom Related opportunity
many2one
field field on the Project model and a custom
Related project
one2many field field on the
Lead model, then provide the following details about the record being duplicated:

小技巧
You can create another automation rule with 更新记录 actions to update the fields of the new or duplicated record if necessary. For example, you can use a Create Record action to create a new project task and then assign it to a specific user using an Update Record action.
创建活动¶
This action is used to schedule a new activity linked to the record.
To define the action:
With Create Activity selected as the Type of action, select the appropriate Activity Type from the dropdown menu.
Enter a Title.
Indicate by when the activity should be completed by specifying a number of Days, Weeks, or Months in the Due Date In field.
Select a User type:
To always assign the activity to the same user, select Specific User, then add the user in the Responsible field.
To target a user linked to the record dynamically, select Dynamic User (based on record). If needed, change the User Field by clicking on the placeholder field name then selecting or searching for the user field in the list that appears. Clicking the (right arrow) next to the field name allows you to access related fields if needed.
Optionally, add a Note to provide more information about the activity.
Example
After a proposition is sent to a opportunity with a high expected revenue, you want to create an activity for the salesperson’s team leader to call the potential client to increase the chances of closing the deal.
To do so, set the Activity Type to Call and the User Type to Dynamic User (based on record). Click on the placeholder field and select Sales Team, then click the (right arrow) and select Team Leader.

发送电子邮件和发送短信¶
These actions are used to send an email or a text message to a contact linked to a specific record.
To define the action:
With Send Email or Send SMS selected as the Type of action, as relevant, select or create an Email Template or SMS Template.
In the Send Email As or Send SMS As field, choose how you want to send the email or text message.
For an email, select:
电子邮件:将信息作为电子邮件发送给 电子邮件模板 的收件人。
消息:在记录上发布消息,并通知记录的关注者。
备注:将消息作为内部备注发送,内部用户可在沟通栏中看到。
For a text message, select:
短信(无备注):将信息作为文本信息发送给 :guilabel:`短信模板`的收件人。
短信(带备注):将短信作为文本消息发送给 短信模板 的收件人,并作为内部备注发布在沟通栏中。
仅限备注:只将消息作为内部备注发布在沟通栏中。
发送 WhatsApp¶
重要
要自动发送 WhatsApp 消息,必须创建一个或多个 WhatsApp 模板。
This action is used to send a WhatsApp message to a contact linked to a specific record.
With Send WhatsApp selected as the Type of action, select the appropriate WhatsApp Template from the dropdown menu.
添加关注者和删除关注者¶
This action is used to subscribe/unsubscribe existing contacts as followers of the record.
With Add Followers or Remove Followers selected as the Type of action, as relevant, select a Followers Type:
To always add/remove the same contact(s), select Specific Followers then select the contact(s) from the dropdown menu. Multiple contacts can be added/removed.
To add/remove a contact linked to the record dynamically, select Dynamic Followers. If needed, change the Followers Field by clicking on the placeholder field name then selecting or searching for the partner field in the list that appears. Clicking the (right arrow) next to the field name allows you to access related fields if needed.
Example
To keep customers informed of progress on a project, this automated action adds the relevant
customer as a follower when a project task is set to In progress
.

执行代码¶
重要
For automation rules that require the execution of custom code, note that maintenance of custom code is not included in the Standard or Custom pricing plans and incurs additional fees.
此操作用于执行 Python 代码。您可以使用以下变量将代码写入 代码 选项卡:
env
:触发操作的环境模型
:触发操作的记录的模型;是一个无效记录集记录
:触发操作的记录;可能无效记录
:在多模式下触发操作的所有记录的记录集;可留空time
、datetime
、dateutil
、timezone
:实用的Python时间处理库float_compare
: utility function to compare floats based on specific precisionlog(message, level='info')
: logging function to record debug information in ir.logging table_logger.info(message)
: logger to emit messages in server logsUserError
: exception class for raising user-facing warning messagesCommand
: x2many commands namespaceaction = {...}
: to return an action
小技巧
The available variables are described both in the Code and Help tabs.
发送 Webhook 通知¶
This action is used to send a POST
API request with the values of the selected Fields
to the webhook URL specified in the URL field.
The Sample Payload provides a preview of the data included in the request using a random record’s data or dummy data if no record is available.
注解
It is also possible to set up an automated action that uses a webhook to receive data from an external system when a predefined event occurs in that system.
Multi Actions¶
This action is used to trigger multiple actions (linked to the current model) at the same time.
To define the actions:
With Multi Actions selected as the Type of action, click Add an action.
In the Add: Child Actions pop-up:
select one or more existing actions and click Select; or
click New, define the action to be executed, then click Save & Close or, to create additional actions, Save & New .
Repeat as many times as needed.
