自动化任务

自动化规则用于根据用户操作(例如,将字段设置为特定值时应用修改)、电子邮件事件、时间条件(例如,在最后一次更新后 7 天存档记录)或外部事件触发自动更改。

使用 Studio 创建自动化规则的步骤如下:

  1. 开启 Studio 并点击 自动化,然后点击 新建

  2. 选择 触发器 并根据所选触发器填写屏幕上出现的字段。

  3. 点击 添加操作,然后选择 操作类型,并根据所选操作,填写屏幕上出现的字段。

  4. 点击 保存并关闭保存并新建

Example

订阅模型中的自动操作示例

小技巧

  • 要修改自动化规则的 模型,请在 Studio 中点击 自动化 之前切换模型,或 激活开发者模式,创建或编辑自动化规则,并在 自动化规则 表单中选择 模型

  • 您也可以从任何看板阶段创建自动化规则,方法是点击看板阶段名称旁边的齿轮图标(),然后选择 自动化。在这种情况下,触发器 默认设置为 阶段设置为,但您可以根据需要进行更改。

    从看板阶段创建自动化

触发器

触发器 用于定义何时应用自动化规则。可用的触发器取决于 模型。共有五个触发器类别:

小技巧

您还可以定义 域名更新前 来指定在触发自动化规则*之前*必须满足的条件。相反,使用 额外条件应用于 筛选器定义的条件会在执行自动化规则*时*进行检查。

要定义 域名更新前激活开发模式,创建或编辑自动化规则,点击 编辑域名,然后点击 新规则

例如,如果要在之前没有地址的联系人设置电子邮件地址时执行自动操作(而不是修改其现有地址),请将 域名更新前 定义为 未设置电子邮件,将 应用于 域名定义为 电子邮件已设置

Example of a trigger with a Before Update Domain

值更新

该类别中可用的触发器取决于模型,并基于常见的字段更改,例如添加特定标记(如添加到任务)或设置 用户 字段。选择触发器,然后根据需要选择值。

值更新触发器示例

电子邮件活动

接收或发送电子邮件时触发自动操作。

时间条件

根据日期字段触发自动操作。可使用以下触发器:

  • 基于日期字段: 选择要用于 延迟 字段旁边的字段。

  • After creation: The action is triggered when a record is created and saved.

  • After last update: The action is triggered when an existing record is edited and saved.

You can then define:

  • a Delay: Specify the number of minutes, hours, days, or months. To trigger the action before the trigger date, specify a negative number. If you selected the Based on date field trigger, you must also select the date field to be used to determine the delay.

  • Extra Conditions: Click Add condition, then specify the conditions to be met to trigger the automation rule. Click New Rule to add another condition.

The action is triggered when the delay is reached and the conditions are met.

Example

If you want to send a reminder email 30 minutes before the start of a calendar event, select the Start (Calendar Event) under Trigger Date and set the Delay to -30 Minutes.

Example of a Based on date field trigger

注解

By default, the scheduler checks for trigger dates every 4 hours, meaning lower granularity in time-based automations may not always be honored.

自定义

Trigger automated actions:

  • On save: When the 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 saving the record.

For the On save 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.

警告

If no field is selected in the When updating field, the automated action may be executed multiple times per record.

Optionally, you can also define additional conditions to be met to trigger the automation rule in the Apply on field.

注解

The On UI change trigger can only be used with the 执行代码 action and only works when a modification is made manually. The action is not executed if the field is changed through another automation rule.

外部

基于 Webhook 触发自动化动作。Webhook 是一种跨系统通信机制,当源系统发生特定事件时,会向目标系统发送 HTTP(S) 请求,该请求通常携带事件相关数据载荷。

要配置 网络钩子 触发器,请将 Odoo 生成的 URL 复制到目标系统(即接收请求的系统)。然后,在 目标记录 字段中输入要运行的代码,以定义使用自动化规则更新的记录。

警告

URL 必须属于**机密**;在网络公开或随意分享可能导致系统遭受恶意攻击。如有必要,请点击 重置密钥 按钮更改 URL 加密凭证。

注解

  • 目标记录 字段中默认定义的代码,适用于来自其他 Odoo 数据库的网络钩子。它用于根据载荷中的信息,确定需要更新的记录。

  • 如果您希望将网络钩子的内容用于查找记录以外的目的(例如*创建*记录),您唯一的选择是使用 执行代码 操作。在这种情况下,:guilabel:`目标记录`字段必须包含任何有效代码,但其结果不会对自动操作本身产生任何影响。

  • 网络钩子的内容可在服务器操作上下文中作为 payload 变量(即包含传入请求的 GET 参数或 POST JSON 主体的字典)使用。

You can also choose to Log Calls to record the payloads received, e.g., to make sure the data sent by the source system matches the expected format and content. This also helps identify and diagnose any issues that may arise. To access the logs, click the Logs smart button at the top of the Automation rules form.

更多内容

Web挂钩

操作

定义自动化规则的 触发器 后,点击 添加操作 以定义要执行的操作。

小技巧

您可以为同一触发器/自动化规则定义多个操作。操作将按照定义的顺序执行。例如,如果您定义了 更新记录 操作,然后又定义了 发送电子邮件 操作,则电子邮件将使用更新后的值。但是,如果在 更新记录 操作之前定义了 发送电子邮件 操作,则电子邮件将使用更新操作运行**之前**设置的值。

更新记录

此操作允许更新记录的一个(相关)字段。点击 更新 字段,在打开的列表中选择或搜索要更新的字段;必要时点击字段名旁的向右箭头,访问相关字段列表。

If you selected a many2many field, choose whether the field must be updated by Adding, Removing, or Setting it to the selected value or by Clearing it.

Example

如果您希望自动操作从客户记录中删除标签,请将 更新 字段设置为 客户 > 标签,选择 通过删除,然后选择标签。

更新记录操作示例

小技巧

Alternatively, you can also set a record’s field dynamically using Python code. To do so, select Compute instead of Update, then enter the code to be used for computing the field’s value. For example, if you want the automation rule to compute a custom datetime field when a task’s priority is set to High (by starring the task), you can define the trigger Priority is set to to High and define the Update Record action as follows:

使用 Python 表达式计算自定义日期时间字段

创建活动

此操作用于安排与记录关联的新活动。选择 活动类型,输入 标题 和描述,然后在 到期日在 字段中指定要安排活动的时间,并选择 用户类型:

  • 要始终将活动分配给同一用户,请选择 特定用户,并在 负责人 字段中添加用户;

  • 要针对动态链接到记录的用户,请选择 动态用户(基于记录),并根据需要更改 用户字段

Example

将潜在客户转化为商机后,您希望自动操作为负责该潜在客户的用户发起呼叫。要执行此操作,请将 活动类型 设为 呼叫,并将 用户类型 设为 动态用户(基于记录)

创建活动操作示例

发送电子邮件和发送短信

这些操作用于向链接到特定记录的联系人发送电子邮件或短信。为此,请选择或创建 电子邮件模板短信模板,然后在 发送电子邮件为发送短信为 字段中选择发送电子邮件或短信的方式:

  • 电子邮件: 将信息作为电子邮件发送给 电子邮件模板 的收件人。

  • 消息: 在记录上发布消息,并通知记录的关注者。

  • 备注: 将消息作为内部备注发送,内部用户可在沟通栏中看到。

  • 短信(无备注): 将信息作为文本信息发送给 :guilabel:`短信模板`的收件人。

  • 短信(带备注): 将短信作为文本消息发送给 短信模板 的收件人,并作为内部备注发布在沟通栏中。

  • 仅限备注: 只将消息作为内部备注发布在沟通栏中。

发送 WhatsApp

重要

To automate the sending of WhatsApp messages, one or more WhatsApp templates must be created.

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 drop-down menu.

Add Followers and Remove Followers

Use these actions to (un)subscribe existing contacts to/from the record.

创建记录

This action is used to create a new record on any model.

Select the required model in the Record to Create field; it contains the current model by default. Specify a Name for the record, and then, if you want to create the record on another model, select a field in the Link Field field to link the record that triggered the creation of the new record.

注解

The dropdown list related to the Link Field field only contains one2many fields existing on the current model that are linked to a many2one field on the target model.

小技巧

You can create another automation rule with 更新记录 actions to update the fields of the new 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 execute Python code. You can write your code into the Code tab using the following variables:

  • env: environment on which the action is triggered

  • model: model of the record on which the action is triggered; is a void recordset

  • record: record on which the action is triggered; may be void

  • records: recordset of all records on which the action is triggered in multi-mode; this may be left empty

  • time, datetime, dateutil, timezone: useful Python libraries

  • float_compare: utility function to compare floats based on specific precision

  • log(message, level='info'): logging function to record debug information in ir.logging table

  • _logger.info(message): logger to emit messages in server logs

  • UserError: exception class for raising user-facing warning messages

  • Command: x2many commands namespace

  • action = {...}: to return an action

小技巧

The available variables are described both in the Code and Help tabs.

发送 Webhook 通知

This action allows to send a POST request with the values of the Fields to the 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.

执行现有行动

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.