在 Azure 逻辑应用中创建控制工作流操作的条件语句Create conditional statements that control workflow actions in Azure Logic Apps

若要仅在通过指定条件后在逻辑应用程序中运行指定操作,请添加条件语句。To run specific actions in your logic app only after passing a specified condition, add a conditional statement. 此控制结构将工作流中的数据与特定值或字段进行比较。This control structure compares the data in your workflow against specific values or fields. 然后,可以指定基于数据是否符合条件运行的不同操作。You can then specify different actions that run based on whether or not the data meets the condition. 可以将一个条件嵌套在另一个条件中。You can nest conditions inside each other.

例如,假设你有一个逻辑应用,当网站的 RSS 源中出现新项时,该应用发送的电子邮件过多。For example, suppose you have a logic app that sends too many emails when new items appear on a website's RSS feed. 你可以添加一个条件语句,以便仅在新项包含特定字符串时才发送电子邮件。You can add a conditional statement to send email only when the new item includes a specific string.

Tip

若要基于不同的特定值运行不同步骤,请改用 switch 语句To run different steps based on different specific values, use a switch statement instead.

先决条件Prerequisites

添加条件Add condition

  1. Azure 门户的逻辑应用设计器中打开逻辑应用。In the Azure portal, open your logic app in Logic App Designer.

  2. 在所需的位置添加条件。Add a condition at the location that you want.

    若要在步骤之间添加条件,请将指针移到要添加条件的箭头上。To add a condition between steps, move the pointer over the arrow where you want to add the condition. 选择出现的加号 (+),然后选择“添加操作”。Choose the plus sign (+) that appears, then choose Add an action. 例如:For example:

    在步骤之间添加操作

    如果想要在工作流末尾添加条件,请在逻辑应用的底部选择“新建步骤”>“添加操作”。When you want to add a condition at the end of your workflow, at the bottom of your logic app, choose New step > Add an action.

  3. 在搜索框中,输入“条件”作为筛选器。In the search box, enter "condition" as your filter. 选择以下操作:条件 - 控制Select this action: Condition - Control

    添加条件

  4. 在“条件”框中,构建条件。In the Condition box, build your condition.

    1. 在左侧框中,指定要比较的数据或字段。In the left box, specify the data or field that you want to compare.

      当你在左侧框内单击时,将显示动态内容列表,以便你可以选择逻辑应用中先前步骤的输出。When you click inside the left box, the dynamic content list appears so you can select outputs from previous steps in your logic app. 对于此示例,请选择“RSS 源摘要”。For this example, select the RSS feed summary.

      构建条件

    2. 在中间的框中,选择要执行的操作。In the middle box, select the operation to perform. 对于此示例,请选择“包含”。For this example, select "contains".

    3. 在右侧框中,指定作为条件的值或字段。In the right box, specify a value or field as your criteria. 对于此示例,请指定此字符串:MicrosoftFor this example, specify this string: Microsoft

    下面是完整条件:Here's the complete condition:

    完整条件

    若要在条件中添加其他行,请选择“添加” > “添加行”。To add another row to your condition, choose Add > Add row. 若要添加包含子条件的组,请选择“添加” > “添加组”。To add a group with subconditions, choose Add > Add group. 若要对现有行进行分组,请选中这些行的复选框,选择任意行的省略号 (...) 按钮,然后选择“生成组”。To group existing rows, select the checkboxes for those rows, choose the ellipses (...) button for any row, and then choose Make group.

  5. 在“如果为 true”和“如果为 false”下,根据是否符合条件添加要执行的步骤。Under If true and If false, add the steps to perform based on whether the condition is met. 例如:For example:

    具有“如果为 true”和“如果为 false”路径的条件

    Tip

    可将现有操作拖动到“如果为 true”和“如果为 false”路径。You can drag existing actions into the If true and If false paths.

  6. 保存逻辑应用。Save your logic app.

现在此逻辑应用仅当 RSS 源中的新项符合条件时才发送邮件。This logic app now sends mail only when the new items in the RSS feed meet your condition.

JSON 定义JSON definition

下面是条件语句背后的高级代码定义:Here's the high-level code definition behind a conditional statement:

"actions": {
  "Condition": {
    "type": "If",
    "actions": {
      "Send_an_email": {
        "inputs": {},
        "runAfter": {}
    },
    "expression": {
      "and": [ 
        { 
          "contains": [ 
            "@triggerBody()?['summary']", 
            "Microsoft"
          ]
        } 
      ]
    },
    "runAfter": {}
  }
},

获取支持Get support

后续步骤Next steps