在 Azure 逻辑应用中使用变量来存储和管理值Store and manage values by using variables in Azure Logic Apps

本文介绍如何创建和使用用于在逻辑应用中存储值的变量。This article shows how to create and work with variables that you use to store values in your logic app. 例如,变量可帮助跟踪某个循环运行的次数。For example, variables can help you track the number of times that a loop runs. 若要循环访问某个数组或检查特定项的数组,可以使用变量来引用每个数组项的索引号。To iterate over an array or check an array for a specific item, you can use a variable to reference the index number for each array item.

可为整数、浮点数、布尔值、字符串、数组和对象等数据类型创建变量。You can create variables for data types such as integer, float, boolean, string, array, and object. 创建变量后,可以执行其他任务,例如:After you create a variable, you can perform other tasks, for example:

  • 获取或引用变量的值。Get or reference the variable's value.
  • 按常量值增大或减小变量,也称为递增和递减。 Increase or decrease the variable by a constant value, also known as increment and decrement.
  • 将不同的值赋给变量。Assign a different value to the variable.
  • 插入或追加变量值作为字符串或数组中的最后一个时间。 Insert or append the variable's value as the last time in a string or array.

变量只在创建它们的逻辑应用实例中存在并保持全局性。Variables exist and are global only within the logic app instance that creates them. 另外,不管在逻辑应用实例中进行什么循环迭代,它们都会保留。Also, they persist across any loop iterations inside a logic app instance. 引用变量时,请使用变量的名称作为令牌,而不要使用操作的名称。操作名称通常用于引用操作的输出。When you reference a variable, use the variable's name as the token, not the action's name, which is the usual way to reference an action's outputs.

Important

默认情况下,“For each”循环中的周期并行运行。By default, cycles in a "For each" loop run in parallel. 在循环中使用变量时,请按顺序运行循环,以便变量返回可预测的结果。When you use variables in loops, run the loop sequentially so that variables return predictable results.

先决条件Prerequisites

初始化变量Initialize variable

可以创建一个变量并声明其数据类型和初始值 - 在逻辑应用中的一个操作内指定所有这些对象。You can create a variable and declare its data type and initial value - all within one action in your logic app. 可以仅在全局级别声明变量,而不是在作用域、条件和循环中声明。You can only declare variables at the global level, not within scopes, conditions, and loops.

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

    本示例使用 Azure 门户,以及包含现有触发器的逻辑应用。This example uses the Azure portal and a logic app with an existing trigger.

  2. 在逻辑应用中要添加变量的步骤下,执行以下步骤之一:In your logic app, under the step where you want to add a variable, follow one of these steps:

    • 若要在最后一个步骤下添加操作,请选择“新建步骤”。 To add an action under the last step, select New step.

      添加操作

    • 若要在步骤之间添加操作,请将鼠标移到连接箭头上方,以显示加号 ( + )。To add an action between steps, move your mouse over the connecting arrow so that the plus sign (+) appears. 选择加号,然后选择“添加操作”。 Select the plus sign, and then select Add an action.

  3. 在“选择操作” 下的搜索框中,输入 variables 作为筛选器。Under Choose an action, in the search box, enter variables as your filter. 在操作列表中,选择“初始化变量”。 From the actions list, select Initialize variable.

    选择操作

  4. 提供有关变量的此信息,如下所述:Provide this information about your variable as described below:

    属性Property 必须Required ValueValue 说明Description
    名称Name Yes <variable-name><variable-name> 要递增的变量的名称The name for the variable to increment
    类型Type Yes <variable-type><variable-type> 变量的数据类型The data type for the variable
    Value No <start-value><start-value> 变量的初始值The initial value for your variable

    提示:尽管此值是可选的,但最好是设置此值,以便始终知道变量的起始值。Tip: Although optional, set this value as a best practice so you always know the start value for your variable.

    例如:For example:

    初始化变量

  5. 现在,继续添加所需的操作。Now continue adding the actions you want. 完成后,请在设计器工具栏上选择“保存” 。When you're done, on the designer toolbar, select Save.

如果从设计器切换到代码视图编辑器,“初始化变量” 操作会采用 JavaScript 对象表示法 (JSON) 格式,按以下方式显示在逻辑应用定义中:If you switch from the designer to the code view editor, here is the way that the Initialize variable action appears in your logic app definition, which is in JavaScript Object Notation (JSON) format:

"actions": {
   "Initialize_variable": {
      "type": "InitializeVariable",
      "inputs": {
         "variables": [ {
               "name": "Count",
               "type": "Integer",
               "value": 0
          } ]
      },
      "runAfter": {}
   }
},

Note

尽管初始化变量操作具有已结构化为数组的 variables 部分,但该操作一次只能创建一个变量。Although the Initialize variable action has a variables section that's structured as an array, the action can create only one variable at a time. 每个新变量都需要单独的初始化变量操作。Each new variable requires an individual Initialize variable action.

下面是其他一些变量类型示例:Here are examples for some other variable types:

字符串变量String variable

"actions": {
   "Initialize_variable": {
      "type": "InitializeVariable",
      "inputs": {
         "variables": [ {
               "name": "myStringVariable",
               "type": "String",
               "value": "lorem ipsum"
          } ]
      },
      "runAfter": {}
   }
},

布尔值变量Boolean variable

"actions": {
   "Initialize_variable": {
      "type": "InitializeVariable",
      "inputs": {
         "variables": [ {
               "name": "myBooleanVariable",
               "type": "Boolean",
               "value": false
          } ]
      },
      "runAfter": {}
   }
},

包含整数的数组Array with integers

"actions": {
   "Initialize_variable": {
      "type": "InitializeVariable",
      "inputs": {
         "variables": [ {
               "name": "myArrayVariable",
               "type": "Array",
               "value": [1, 2, 3]
          } ]
      },
      "runAfter": {}
   }
},

包含字符串的数组Array with strings

"actions": {
   "Initialize_variable": {
      "type": "InitializeVariable",
      "inputs": {
         "variables": [ {
               "name": "myArrayVariable",
               "type": "Array",
               "value": ["red", "orange", "yellow"]
          } ]
      },
      "runAfter": {}
   }
},

获取变量的值Get the variable's value

若要检索或引用变量的内容,也可以在逻辑应用设计器和代码视图编辑器中使用 variables() 函数To retrieve or reference a variable's contents, you can also use the variables() function in the Logic App Designer and the code view editor. 引用变量时,请使用变量的名称作为令牌,而不要使用操作的名称。操作名称通常用于引用操作的输出。When referencing a variable, use the variable's name as the token, not the action's name, which is the usual way to reference an action's outputs.

例如,此表达式使用 variables() 函数,从本文前面创建的数组变量中获取项。For example, this expression gets the items from the array variable created previously in this article by using the variables() function. string() 函数以字符串格式返回变量的内容:"1, 2, 3, red"The string() function returns the variable's contents in string format: "1, 2, 3, red"

@{string(variables('myArrayVariable'))}

递增变量Increment variable

若要按常量值增大或递增某个变量,请将“递增变量”操作添加到逻辑应用。 To increase or increment a variable by a constant value, add the Increment variable action to your logic app. 此操作仅适用于整数和浮点数变量。This action works only with integer and float variables.

  1. 在逻辑应用设计器中要用于增大现有变量的步骤下,选择“新建步骤” 。In the Logic App Designer, under the step where you want to increase an existing variable, select New step.

    例如,此逻辑应用已包含一个触发器,以及一个创建了变量的操作。For example, this logic app already has a trigger and an action that created a variable. 因此,请在以下步骤下添加新操作:So, add a new action under these steps:

    添加操作

    若要在现有步骤之间添加操作,请将鼠标移到连接箭头上方,以显示加号 (+)。To add an action between existing steps, move your mouse over the connecting arrow so that the plus sign (+) appears. 选择加号,然后选择“添加操作”。 Select the plus sign, and then select Add an action.

  2. 在搜索框中,输入“递增变量”作为筛选器。In the search box, enter "increment variable" as your filter. 在操作列表中,选择“递增变量”。 In the actions list, select Increment variable.

    选择“递增变量”操作

  3. 提供用于递增变量的以下信息:Provide this information for incrementing your variable:

    属性Property 必须Required ValueValue 说明Description
    名称Name Yes <variable-name><variable-name> 要递增的变量的名称The name for the variable to increment
    Value No <increment-value><increment-value> 用于递增变量的值。The value used for incrementing the variable. 默认值为 1。The default value is one.

    提示:尽管此值是可选的,但最好是设置此值,以便始终知道用于递增变量的特定值。Tip: Although optional, set this value as a best practice so you always know the specific value for incrementing your variable.

    例如:For example:

    递增值的示例

  4. 完成后,请在设计器工具栏上选择“保存” 。When you're done, on the designer toolbar, select Save.

如果从设计器切换到代码视图编辑器,“递增变量” 操作会采用 JSON 格式,按以下方式显示在逻辑应用定义中:If you switch from the designer to the code view editor, here is the way that the Increment variable action appears inside your logic app definition, which is in JSON format:

"actions": {
   "Increment_variable": {
      "type": "IncrementVariable",
      "inputs": {
         "name": "Count",
         "value": 1
      },
      "runAfter": {}
   }
},

示例:创建循环计数器Example: Create loop counter

变量通常用于统计某个循环的运行次数。Variables are commonly used for counting the number of times that a loop runs. 本示例通过创建一个统计电子邮件中附件数的循环,来演示如何创建和使用变量完成此任务。This example shows how you create and use variables for this task by creating a loop that counts the attachments in an email.

  1. 在 Azure 门户中,创建一个空白逻辑应用。In the Azure portal, create a blank logic app. 添加一个触发器,用于检查新电子邮件和任何附件。Add a trigger that checks for new email and any attachments.

    本示例使用 Office 365 Outlook 触发器“收到新电子邮件时”。 This example uses the Office 365 Outlook trigger for When a new email arrives. 可将此触发器设置为仅当电子邮件包含附件时才触发。You can set up this trigger to fire only when the email has attachments. 但是,可以使用任何可检查包含附件的新电子邮件的连接器,例如 Outlook.com 连接器。However, you can use any connector that checks for new emails with attachments, such as the Outlook.com connector.

  2. 在触发器中,若要检查附件并将这些附件传入逻辑应用的工作流,请针对以下属性选择“是” :In the trigger, to check for attachments and pass those attachments into your logic app's workflow, select Yes for these properties:

    • 带有附件Has Attachment
    • 包括附件Include Attachments

    检查和包括附件

  3. 添加“初始化变量”操作Add the Initialize variable action. 创建名为 Count、起始值为 0 的一个整数变量。Create an integer variable named Count that has a zero start value.

    为“初始化变量”添加操作

  4. 若要循环浏览每个附件,请添加 for each 循环。To cycle through each attachment, add a for each loop.

    1. 在“初始化变量”操作下,选择“新建步骤” 。Under the Initialize variable action, select New step.

    2. 在“选择操作”下,选择“内置”。 Under Choose an action, select Built-in. 在搜索框中,输入 for each 作为搜索筛选器,然后选择 For eachIn the search box, enter for each as your search filter, and select For each.

      添加“for each”循环

  5. 在该循环中,单击“从先前的步骤中选择一个输出”框。 In the loop, click inside the Select an output from previous steps box. 当动态内容列表出现时,选择“附件”。 When the dynamic content list appears, select Attachments.

    选择“附件”

    “附件”属性向循环中传入一个数组,该数组包含触发器输出中的电子邮件附件。 The Attachments property passes an array, which has the email attachments from the trigger's output, into your loop.

  6. 在“For each”循环中,选择“添加操作”。 In the For each loop, select Add an action.

    选择“添加操作”

  7. 在搜索框中,输入“递增变量”作为筛选器。In the search box, enter "increment variable" as your filter. 在操作列表中,选择“递增变量”。 From the actions list, select Increment variable.

    Note

    确保“递增变量”操作显示在循环内部。 Make sure that the Increment variable action appears inside the loop. 如果该操作显示在循环外部,请将其拖到循环中。If the action appears outside the loop, drag the action into the loop.

  8. 在“递增变量”操作中,从“名称”列表中选择“Count”变量。 In the Increment variable action, from the Name list, select the Count variable.

    选择“Count”变量

  9. 在循环下,添加用于发送附件数目的任何操作。Under the loop, add any action that sends you the number of attachments. 在操作中,包含来自 Count 变量的值,例如:In your action, include the value from the Count variable, for example:

    添加用于发送结果的操作

  10. 保存逻辑应用。Save your logic app. 在设计器工具栏上选择“保存”。 On the designer toolbar, select Save.

测试逻辑应用Test your logic app

  1. 如果未启用逻辑应用,请在逻辑应用菜单中选择“概览”。 If your logic app isn't enabled, on your logic app menu, select Overview. 在工具栏中选择“启用”。 On the toolbar, select Enable.

  2. 在逻辑应用设计器工具栏上,选择“运行” 。On the Logic App Designer toolbar, select Run. 此步骤会手动启动逻辑应用。This step manually starts your logic app.

  3. 将包含一个或多个附件的电子邮件发送到本示例中使用的电子邮件帐户。Send an email with one or more attachments to the email account you used in this example.

    此步骤会触发逻辑应用的触发器,而该触发器会创建并运行逻辑应用工作流的实例。This step fires the logic app's trigger, which creates and runs an instance for your logic app's workflow. 因此,逻辑应用会发送消息或电子邮件,其中显示了发送的电子邮件中的附件数目。As a result, the logic app sends you a message or email that shows the number of attachments in the email you sent.

如果从设计器切换到代码视图编辑器,For each 循环与“递增变量” 操作会采用 JSON 格式,按以下方式显示在逻辑应用定义中。If you switch from the designer to the code view editor, here is the way that the For each loop appears along with the Increment variable action inside your logic app definition, which is in JSON format.

"actions": {
   "For_each": {
      "type": "Foreach",
      "actions": {
         "Increment_variable": {
           "type": "IncrementVariable",
            "inputs": {
               "name": "Count",
               "value": 1
            },
            "runAfter": {}
         }
      },
      "foreach": "@triggerBody()?['Attachments']",
      "runAfter": {
         "Initialize_variable": [ "Succeeded" ]
      }
   }
},

递减变量Decrement variable

若要按常量值减小或递减变量,请遵循增大变量的步骤,不过,需要找到并选择“递减变量”操作。 To decrease or decrement a variable by a constant value, follow the steps for increasing a variable except that you find and select the Decrement variable action instead. 此操作仅适用于整数和浮点数变量。This action works only with integer and float variables.

下面是“递减变量”操作的属性: Here are the properties for the Decrement variable action:

属性Property 必须Required ValueValue 说明Description
名称Name Yes <variable-name><variable-name> 要递减的变量的名称The name for the variable to decrement
Value No <increment-value><increment-value> 用于递减变量的值。The value for decrementing the variable. 默认值为 1。The default value is one.

提示:尽管此值是可选的,但最好是设置此值,以便始终知道用于递减变量的特定值。Tip: Although optional, set this value as a best practice so you always know the specific value for decrementing your variable.

如果从设计器切换到代码视图编辑器,“递减变量”操作会采用 JSON 格式,按以下方式显示在逻辑应用定义中。 If you switch from the designer to the code view editor, here is the way that the Decrement variable action appears inside your logic app definition, which is in JSON format.

"actions": {
   "Decrement_variable": {
      "type": "DecrementVariable",
      "inputs": {
         "name": "Count",
         "value": 1
      },
      "runAfter": {}
   }
},

设置变量Set variable

若要将不同的值赋给现有变量,请遵循增大变量的步骤,不过需要:To assign a different value to an existing variable, follow the steps for increasing a variable except that you:

  1. 改为找到并选择“设置变量”操作。 Find and select the Set variable action instead.

  2. 提供变量名称,以及要赋的值。Provide the variable name and value you want to assign. 新值和变量的数据类型必须相同。Both the new value and the variable must have the same data type. 之所以需要该值,是因为此操作没有默认值。The value is required because this action doesn't have a default value.

下面是“设置变量”操作的属性: Here are the properties for the Set variable action:

属性Property 必须Required ValueValue 说明Description
名称Name Yes <variable-name><variable-name> 要更改的变量的名称The name for the variable to change
Value Yes <new-value><new-value> 要赋给变量的值。The value you want to assign the variable. 两者的数据类型必须相同。Both must have the same data type.

Note

除非递增或递减变量,否则更改循环中的变量可能会产生意外的结果,因为循环在默认情况下是并行或并发运行的。 Unless you're incrementing or decrementing variables, changing variables inside loops might create unexpected results because loops run in parallel, or concurrently, by default. 对于这种情况,请尝试将循环设置为按顺序运行。For these cases, try setting your loop to run sequentially. 例如,若要引用循环中的变量值,并且预期该循环实例开头和末尾的值相同,请遵循以下步骤更改循环的运行方式:For example, when you want to reference the variable value inside the loop and expect same value at the start and end of that loop instance, follow these steps to change how the loop runs:

  1. 在循环的右上角选择省略号 (...) 按钮,然后选择“设置”。 In your loop's upper-right corner, select the ellipsis (...) button, and then select Settings.

  2. 在“并发控制”下,将“替代默认值”设置更改为“打开”。 Under Concurrency Control, change the Override Default setting to On.

  3. 将“并行度”滑块拖到 1Drag the Degree of Parallelism slider to 1.

如果从设计器切换到代码视图编辑器,“设置变量”操作会采用 JSON 格式,按以下方式显示在逻辑应用定义中。 If you switch from the designer to the code view editor, here is the way that the Set variable action appears inside your logic app definition, which is in JSON format. 本示例将 Count 变量的当前值更改为其他值。This example changes the Count variable's current value to another value.

"actions": {
   "Initialize_variable": {
      "type": "InitializeVariable",
      "inputs": {
         "variables": [ {
               "name": "Count",
               "type": "Integer",
               "value": 0
          } ]
      },
      "runAfter": {}
   },
   "Set_variable": {
      "type": "SetVariable",
      "inputs": {
         "name": "Count",
         "value": 100
      },
      "runAfter": {
         "Initialize_variable": [ "Succeeded" ]
      }
   }
},

追加到变量Append to variable

对于存储字符串或数组的变量,可以插入或追加一个变量值,作为这些字符串或数组中的最后一个项。 For variables that store strings or arrays, you can insert or append a variable's value as the last item in those strings or arrays. 可以遵循增大变量的步骤,不过,需要做出以下变化:You can follow the steps for increasing a variable except that you follow these steps instead:

  1. 根据变量是字符串还是数组,找到并选择以下操作之一:Find and select one of these actions based on whether your variable is a string or an array:

    • 追加到字符串变量Append to string variable
    • 追加到数组变量Append to array variable
  2. 提供要追加为字符串或数组中最后一个项的值。Provide the value to append as the last item in the string or array. 此值是必需的。This value is required.

下面是“追加到...”操作的属性: Here are the properties for the Append to... actions:

属性Property 必须Required ValueValue 说明Description
名称Name Yes <variable-name><variable-name> 要更改的变量的名称The name for the variable to change
Value Yes <append-value><append-value> 要追加的值,可以是任何类型The value you want to append, which can have any type

如果从设计器切换到代码视图编辑器,“追加到数组变量”操作会采用 JSON 格式,按以下方式显示在逻辑应用定义中。 If you switch from the designer to the code view editor, here is the way that the Append to array variable action appears inside your logic app definition, which is in JSON format. 本示例创建一个数组变量,并将另一个值添加为数组中的最后一个项。This example creates an array variable, and adds another value as the last item in the array. 结果是包含此数组的已更新变量:[1,2,3,"red"]Your result is an updated variable that contains this array: [1,2,3,"red"]

"actions": {
   "Initialize_variable": {
      "type": "InitializeVariable",
      "inputs": {
         "variables": [ {
            "name": "myArrayVariable",
            "type": "Array",
            "value": [1, 2, 3]
         } ]
      },
      "runAfter": {}
   },
   "Append_to_array_variable": {
      "type": "AppendToArrayVariable",
      "inputs": {
         "name": "myArrayVariable",
         "value": "red"
      },
      "runAfter": {
        "Initialize_variable": [ "Succeeded" ]
      }
   }
},

后续步骤Next steps