自动化规则¶
自动化规则允许针对特定触发执行一个或多个预定义操作,例如,当某个字段设置为特定值时创建活动,或在最后一次更新 7 天后归档记录。
创建自动化规则时,域筛选器 允许您添加自动化规则运行必须满足的条件,例如,机会必须分配给特定的销售人员,或记录的状态必须不是 草稿。
使用 Odoo Studio 创建自动化规则的步骤如下:
开启 Studio 并点击 自动化,然后点击 新建。
给自动化规则起一个清晰、有意义的名字,以明确其目的。
选择 触发器 并根据所选触发器填写屏幕上出现的字段。
点击 添加操作,然后选择 操作 的 类型,并根据所选操作,填写屏幕上出现的字段。
点击 保存并关闭 或 保存并新建。
Example
为确保跟进满意度较低的客户,该自动化规则会在销售订单创建 3 个月后为满意度低于 30% 的客户创建活动。

触发器¶
触发器 用于定义自动化规则运行所需的活动类型。可用的触发器取决于 模型。共有五个触发器类别:
添加条件¶
域筛选器允许您确定自动化规则应针对或排除的记录。高效筛选可避免对不受规则影响的记录进行不必要的处理,从而提高整体性能。
小技巧
在创建自动化规则前激活开发者模式,以便最灵活地添加域筛选器。
根据所选触发器的不同,可以定义一个或多个记录在触发*前*和/或触发*后*必须满足的条件。
域名更新前 定义了记录在触发事件发生*之前*必须满足的条件,例如,记录必须具有
类型 = 客户发票
和状态 = 已发布
。在 启动开发者模式 下,点击 编辑域名`(如有),然后点击 :guilabel:`新规则。
额外条件 或在某些情况下 应用于 筛选器定义触发事件发生后记录必须满足的条件,例如,客户发票必须具有
付款状态 = 部分支付
。如需,在 启动开发者模式 下点击 添加条件 或 编辑域,然后点击 新规则。
当发生 触发条件。
Example
如果是个人而非公司的现有联系人首次设置电子邮件地址(与修改电子邮件地址相反)时应执行自动操作,则使用 电子邮件未设置`和 `公司未设置`作为 :guilabel:`域名更新前
,并使用 电子邮件已设置`作为 :guilabel:`应用
域。

注解
The Before Update Domain is not checked upon the creation of a record.
值更新¶
当数据库中发生特定变化时触发自动操作。这类触发器取决于模型,并基于常见的更改,如添加特定标记(如添加到任务中)或设置字段值(如设置 用户 字段)。
选择触发器,然后根据需要选择数值。
电子邮件活动¶
接收或发送电子邮件时触发自动操作。
时间条件¶
在与日期字段或记录的创建或更新相关的时间点触发自动操作。可使用以下触发器:
基于日期字段:在所选日期字段的日期之前或之后的一段时间内触发操作。
创建后:在创建并保存记录后的一段时间内触发该操作。
上次更新后:在编辑并保存现有记录一段时间后触发该操作。
然后,您可以定义:
延迟:指定分钟、小时、天或月的数量。要在触发日期前执行操作,请指定负数。如果选择了 基于日期字段 触发器,还必须选择用于确定延迟的日期字段。
注解
默认情况下,调度程序每 240 分钟或 4 小时检查一次时间触发的自动化规则。对于订单日期后 3 个月或上次更新后 7 天等延迟情况,这个频率一般就足够了。
对于少于相当于 2400 分钟或 40 小时的延迟,系统会重新计算该检查的频率,以确保尽可能遵守更细化的延迟,如事件开始日期和时间前 1 小时或创建后 30 分钟。
屏幕上会显示一条信息,说明计划触发规则后可能出现的延迟。
要查看或手动编辑调度程序的频率,在 启动开发者模式 的情况下,进入 查看数据库的所有调度操作。
在搜索栏中输入`自动化`,然后在结果列表中点击 自动化规则:检查和执行。如果需要,更新 执行每个 字段的值。随时点击 手动运行,手动触发此预定操作。
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.
Example
To send a reminder email 30 minutes before the start of a calendar event, select Based on date
field
as the Trigger, then set the Delay to -30
Minutes
and select Start (Calendar Event) as the date field.

自定义¶
触发自动操作:
On save: when a record is saved.
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.
对于 :guilabel:`保存时`和 :guilabel:`当 UI 变更时`这两种触发器,您**必须**在 :guilabel:`更新时`字段中选择一个或多个用于触发此自动化规则的字段。
警告
如果在 :guilabel:`更新时`字段中没有选择任何字段,该自动化操作可能会对同一条记录执行多次。
(可选)您还可以在 :guilabel:`应用于`字段中定义额外的触发条件。
Example
要触发记录创建时的自动化操作(例如,当创建新联系人时),请选择 保存时 <studio/automated-actions/trigger-custom>`触发器,并使用 `ID 未设置`作为 :guilabel:`更新前作用域,使用 ID 已设置`作为 :guilabel:`应用范围
。
保存新联系人时,会自动为其分配一个数据库 ID,从而触发自动化规则。

外部¶
使用 网络钩子,根据外部系统或应用程序中的特定事件触发自动操作。
在 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 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.
更新记录¶
此操作更新记录的一个(相关)字段。点击 更新 字段,在打开的列表中选择或搜索要更新的字段。如果需要,点击字段名旁的 (右箭头) 以访问相关字段列表。
如果您选择了 many2many 字段,请选择是否必须通过 添加、删除、设置为`选定值或 :guilabel: 清除`来更新该字段。
Example
如果您希望自动操作从客户记录中删除标签,请将 更新 字段设置为 客户 > 标签,选择 通过删除,然后选择标签。

创建活动¶
This action is used to schedule a new activity linked to the record. Select an Activity Type, enter a Title and description, then specify when you want the activity to be scheduled in the Due Date In field, and select a User Type:
To always assign the activity to the same user, select Specific User, then add the user in the Responsible field.
要针对动态链接到记录的用户,请选择 动态用户(基于记录),并根据需要更改 用户字段。
Example
将潜在客户转化为商机后,您希望自动操作为负责该潜在客户的用户发起呼叫。要执行此操作,请将 活动类型 设为 呼叫,并将 用户类型 设为 动态用户(基于记录)。

发送电子邮件和发送短信¶
这些操作用于向链接到特定记录的联系人发送电子邮件或短信。为此,请选择或创建 电子邮件模板 或 短信模板,然后在 发送电子邮件为 或 发送短信为 字段中选择发送电子邮件或短信的方式:
电子邮件:将信息作为电子邮件发送给 电子邮件模板 的收件人。
消息:在记录上发布消息,并通知记录的关注者。
备注:将消息作为内部备注发送,内部用户可在沟通栏中看到。
短信(无备注):将信息作为文本信息发送给 :guilabel:`短信模板`的收件人。
短信(带备注):将短信作为文本消息发送给 短信模板 的收件人,并作为内部备注发布在沟通栏中。
仅限备注:只将消息作为内部备注发布在沟通栏中。
发送 WhatsApp¶
重要
要自动发送 WhatsApp 消息,必须创建一个或多个 WhatsApp 模板。
This action is used to send a WhatsApp message to a contact linked to a specific record. To do so, select the appropriate WhatsApp Template from the dropdown menu.
添加关注者和删除关注者¶
These actions are used to subscribe/unsubscribe existing contacts to/from the record. With Add Followers or Remove Followers selected as the Type of action, as relevant, select a Followers Type:
Specific Followers allows the same, specific contact(s) to be added/removed. To add followers, in Followers to add, select the relevant contact(s) from the dropdown. To remove followers, click the (cross) beside a contact’s name.
Dynamic Followers allows contacts linked to the record to be added/removed dynamically. To add/remove followers dynamically, in Followers Field, enter the technical name of the relevant field.
小技巧
To find a field’s technical name, with developer mode activated, use one of the following methods:
In a record of the relevant model, hover over the tooltip beside the field name. The field’s technical name is found beside Field.
Go to Field Name column of the Field tab.
and open the relevant model. The technical name of the model’s fields is found in the
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. The technical name
of the customer field, i.e., partner_id
is used.

创建记录¶
此操作用于在任何模型上创建新记录。
在 记录创建 字段中选择所需的模型;默认情况下它包含当前模型。为记录指定 名称,然后,如果要在其他模型上创建记录,请在 链接字段 字段中选择一个字段,以链接触发创建新记录的记录。
注解
与 链接字段 字段相关的下拉列表只包含当前模型上存在的 one2many 字段 字段,这些字段与目标模型上的 many2one 字段 字段链接。
小技巧
您可以使用 更新记录 操作创建另一个自动化规则,以便在必要时更新新记录的字段。例如,您可以使用 创建记录 操作创建新项目任务,然后使用 更新记录 操作将其分配给特定用户。
执行代码¶
重要
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.
执行现有行动¶
The action is used to trigger multiple actions (linked to the current model) at the same time. To do so, click on Add a line, then, in the Add: Child Actions pop-up, select an existing action or click New to create a new one.