基于 Azure 逻辑应用中的特定值创建运行工作流操作的 Switch 语句Create switch statements that run workflow actions based on specific values in Azure Logic Apps

若要基于对象、表达式或令牌的值运行特定操作,请添加 Switch 语句。To run specific actions based on the values of objects, expressions, or tokens, add a switch statement. 此结构计算对象、表达式或令牌的值,选择与结果匹配的事例,并仅运行该事例的步骤。This structure evaluates the object, expression, or token, chooses the case that matches the result, and runs specific actions only for that case. switch 语句运行时,应只有一个 case 与结果相匹配。When the switch statement runs, only one case should match the result.

例如,假设你想要一个根据电子邮件中所选的选项执行不同步骤的逻辑应用。For example, suppose you want a logic app that takes different steps based on an option selected in email. 在此示例中,逻辑应用将检查网站的 RSS 源中是否有新内容。In this example, the logic app checks a website's RSS feed for new content. 当新项出现在 RSS 源中时,逻辑应用将向审批者发送电子邮件。When a new item appears in the RSS feed, the logic app sends email to an approver. 逻辑应用将根据审批者选择“批准”还是“拒绝”,执行不同的步骤。Based on whether the approver selects "Approve" or "Reject", the logic app follows different steps.

Tip

与所有的编程语言相同,Switch 语句仅支持相等运算符。Like all programming languages, switch statements support only equality operators. 如果需要其他关系运算符(例如“大于”),请使用条件语句If you need other relational operators, such as "greater than", use a conditional statement. 为了确保确定性的执行行为,case 必须包含唯一且静态的值,而不是动态令牌或表达式。To ensure deterministic execution behavior, cases must contain a unique and static value instead of dynamic tokens or expressions.

先决条件Prerequisites

  • Azure 订阅。An Azure subscription. 如果没有订阅,可以注册 Azure 试用帐户If you don't have a subscription, sign up for a trial Azure account.

  • 若要执行本文中的示例,请使用 Outlook.com 或 Office 365 Outlook 帐户创建此示例逻辑应用To follow the example in this article, create this sample logic app with an Outlook.com or Office 365 Outlook account.

    1. 添加用于发送电子邮件的操作时,请改为查找并选择此操作:发送审批电子邮件When you add the action to send email, find and select this action instead: Send an approval email

      选择“发送审批电子邮件”

    2. 提供必填字段,例如接收审批电子邮件的人员的电子邮件地址。Provide the required fields, like the email address for the person who gets the approval email. 在“用户选项”下,输入“批准、拒绝”。Under User Options, enter "Approve, Reject".

      输入电子邮件详细信息

添加 switch 语句Add switch statement

  1. 对于此示例,请在示例工作流的末尾添加 switch 语句。For this example, add a switch statement at the end your sample workflow. 完成最后一步后,选择“新建步骤”。After the last step, choose New step.

    如果想要在步骤之间添加 switch 语句,请将指针移到要添加 switch 语句的箭头上。When you want to add a switch statement between steps, move the pointer over the arrow where you want to add the switch statement. 选择出现的加号 (+),然后选择“添加操作”。Choose the plus sign (+) that appears, then choose Add an action.

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

    添加 switch

    此时将显示一个带有一个 case 和一个 default case 的 Switch 语句。A switch statement appears with one case and a default case. 默认情况下,switch 语句至少需要一个 case 以及 default case。By default, a switch statement requires at least one case plus the default case.

    空的默认 switch 语句

  3. 在“基于”框中单击,以显示动态内容列表。Click inside the On box so that the dynamic content list appears. 从该列表中选择 SelectedOption 字段,其输出确定要执行的操作。From that list, select the SelectedOption field whose output determines the action to perform.

    选择“SelectedOption”

  4. 若要处理审批者选择 ApproveReject的情况,请在 CaseDefault 之间添加另一个 case。To handle the cases where the approver selects Approve or Reject, add another case between Case and Default.

    添加另一个 case

  5. 将这些操作添加到相应的 case:Add these actions to the corresponding cases:

    Case #Case # SelectedOptionSelectedOption 操作Action
    Case 1Case 1 批准Approve 添加 Outlook 发送电子邮件操作,用于仅在审批者选择了“批准”时才发送有关该 RSS 项的详细信息。Add the Outlook Send an email action for sending details about the RSS item only when the approver selected Approve.
    Case 2Case 2 拒绝Reject 添加 Outlook 发送电子邮件操作,用于通知其他审批者该 RSS 项已被拒绝。Add the Outlook Send an email action for notifying other approvers that the RSS item was rejected.
    默认Default None 无需执行任何操作。No action necessary. 在此示例中,Default case 为空,因为 SelectedOption 只有两个选项。In this example, the Default case is empty because SelectedOption has only two options.

    已完成的 switch 语句

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

    若要手动测试此示例,请选择“运行”,直到逻辑应用找到新 RSS 项并发送审批电子邮件。To manually test this example, choose Run until the logic app finds a new RSS item and sends an approval email. 选择“批准”以观察结果。Select Approve to observe the results.

JSON 定义JSON definition

现已使用 Switch 语句创建了逻辑应用,让我们来看一下 Switch 语句背后的高级代码定义。Now that you created a logic app using a switch statement, let's look at the high-level code definition behind the switch statement.

"Switch": {
   "type": "Switch",
   "expression": "@body('Send_approval_email')?['SelectedOption']",
   "cases": {
      "Case" : {
         "actions" : {
           "Send_an_email": { }
         },
         "case" : "Approve"
      },
      "Case_2" : {
         "actions" : {
           "Send_an_email_2": { }
         },
         "case" : "Reject"
      }
   },
   "default": {
      "actions": {}
   },
   "runAfter": {
      "Send_approval_email": [
         "Succeeded"
      ]
   }
}
LabelLabel 说明Description
"Switch" Switch 语句的名称,可对其进行重命名以提高可读性The name of the switch statement, which you can rename for readability
"type": "Switch" 指示该操作是一个 Switch 语句Specifies that the action is a switch statement
"expression" 在此示例中,指定了审批者选择的选项,稍后将根据其对定义中声明的每个 case 进行求值In this example, specifies the approver's selected option that's evaluated against each case as declared later in the definition
"cases" 可以定义任意数量的 case。Defines any number of cases. 对于每个 case,"Case_*" 是该 case 的默认名称,可重命名以提高可读性For each case, "Case_*" is the default name for that case, which you can rename for readability
"case" 指定 case 的值,该值必须是 switch 语句用于比较的常量和唯一值。Specifies the case's value, which must be a constant and unique value that the switch statement uses for comparison. 如果没有 case 与 switch 表达式结果匹配,请将运行 "default" 部分中的操作。If no cases match the switch expression result, the actions in the "default" section are run.

获取支持Get support

后续步骤Next steps