自動化規則

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

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

  1. 開啟 Studio ,按一下 自動化,然後按 新增

  2. 選擇 觸發器 。若畫面因應所選觸發器而顯示有欄位需要填寫,請填入相關資料。

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

  4. 按一下 儲存並關閉儲存及新增

Example

訂閱模型自動化操作範例

小訣竅

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

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

    在看板階段中建立自動化規則

觸發器

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

小訣竅

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

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

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

設有更新前範圍的觸發範例

資料值更新

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

值更新触发器示例

電郵活動

接收或發送電子郵件時,觸發自動操作。

時間條件

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

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

  • 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.

然後,你可進一步定義:

  • 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.

基於日期欄位觸發範例

備註

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

自訂

觸發自動化操作:

  • 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.

警告

更新此項時 欄位未有選擇任何欄位,該自動化操作可能會在每一項記錄上都執行多次。

你也可選擇在 套用至 欄位中,設定觸發自動化規則需要符合的其他條件。

備註

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.

應採取的行動

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

小訣竅

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

更新記錄

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

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

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

建立活動操作範例

傳送電子郵件及 SMS 短訊

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

  • 電郵:以電子郵件形式,將訊息發送給 電郵範本 的收件人。

  • 訊息:在記錄中發佈訊息,並通知該記錄的關注者。

  • 備註:以內部備註形式發送訊息,內部使用者可在聊天欄看到。

  • 短訊(不加備註):以 SMS 文字訊息形式,將訊息傳送給 短訊範本 的收件人。

  • 短訊(加入備註):以 SMS 文字訊息形式,將訊息傳送給 短訊範本 的收件人,並以內部備註形式,發佈至聊天欄。

  • 只限備註:訊息只會作為內部備註發佈至聊天欄。

加入及移除關注者

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.

執行程式碼

此操作用於執行 Python 程式碼。 你可在 程式碼 分頁中,編寫你的自訂程式碼,並可使用以下變數:

  • env:觸發操作的環境

  • model:觸發操作所針對的記錄的模型,是空白的記錄集

  • record:觸發操作所針對的記錄,可以是空集

  • records:在多模式下觸發操作所針對的所有記錄的記錄集。此項可留空

  • timedatetimedateutiltimezone :有用的 Python 程式庫(時間、日期時間、日期公用程式、時區)

  • float_compare:公用程式函數,按指定精確度比較浮點數

  • log(message, level='info'):在 ir.logging 表格中,記錄除錯資訊的系統日誌寫入函數

  • _logger.info(message):在伺服器日誌中發佈訊息的系統日誌寫入工具

  • UserError:在面向使用者的警告訊息中,標示錯誤的異常類

  • Command:x2many 指令命名空間

  • action = {...}:傳回一項操作

小訣竅

程式碼幫助 分頁都有關於可用變數的說明。

傳送網絡鈎子(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.