Create and manage variables for storing and passing values - Azure Logic Apps (2024)

  • Article
  • 11 minutes to read

Applies to: Azure Logic Apps (Consumption)

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 item 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

By default, cycles in a "For each" loop run in parallel. When you use variables in loops,run the loop sequentiallyso that variables return predictable results.

Prerequisites

  • An Azure subscription. If you don't have subscription, sign up for a free Azure account.

  • The logic app where you want to create the variable

    If you're new to logic apps, review What is Azure Logic Apps? and Quickstart: Create your first logic app.

  • A trigger as the first step in your logic app

    Before you can add actions for creating and working with variables, your logic app must start with a trigger.

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. In the Azure portal or Visual Studio, open your logic app in the Logic App Designer.

    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.

      Create and manage variables for storing and passing values - Azure Logic Apps (1)

    • 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. Under Choose an action, in the search box, enter variables as your filter. From the actions list, select Initialize variable.

    Create and manage variables for storing and passing values - Azure Logic Apps (2)

  4. Provide this information about your variable as described below:

    PropertyRequiredValueDescription
    NameYes<variable-name>The name for the variable to increment
    TypeYes<variable-type>The data type for the variable
    ValueNo<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:

    Create and manage variables for storing and passing values - Azure Logic Apps (3)

  5. Now continue adding the actions you want. When you're done, on the designer toolbar, select Save.

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

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

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.

For example, this expression gets the items from the array variable created previously in this article by using the variables() function. 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:

    Create and manage variables for storing and passing values - Azure Logic Apps (4)

    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.

    Create and manage variables for storing and passing values - Azure Logic Apps (5)

  3. Provide this information for incrementing your variable:

    PropertyRequiredValueDescription
    NameYes<variable-name>The name for the variable to increment
    ValueNo<increment-value>The value used for incrementing the variable. 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:

    Create and manage variables for storing and passing values - Azure Logic Apps (6)

  4. When you're done, on the designer toolbar, select Save.

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. In the Azure portal, create a blank logic app. Add a trigger that checks for new email and any attachments.

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

    Create and manage variables for storing and passing values - Azure Logic Apps (7)

  3. Add the Initialize variable action. Create an integer variable named Count that has a zero start value.

    Create and manage variables for storing and passing values - Azure Logic Apps (8)

  4. 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. In the search box, enter for each as your search filter, and select For each.

      Create and manage variables for storing and passing values - Azure Logic Apps (9)

  5. In the loop, click inside the Select an output from previous steps box. When the dynamic content list appears, select Attachments.

    Create and manage variables for storing and passing values - Azure Logic Apps (10)

    The Attachments property passes an array, which has the email attachments from the trigger's output, into your loop.

  6. In the For each loop, select Add an action.

    Create and manage variables for storing and passing values - Azure Logic Apps (11)

  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. In the Increment variable action, from the Name list, select the Count variable.

    Create and manage variables for storing and passing values - Azure Logic Apps (12)

  9. Under the loop, add any action that sends you the number of attachments. In your action, include the value from the Count variable, for example:

    Create and manage variables for storing and passing values - Azure Logic Apps (13)

  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.

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:

PropertyRequiredValueDescription
NameYes<variable-name>The name for the variable to decrement
ValueNo<increment-value>The value for decrementing the variable. 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.

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:

PropertyRequiredValueDescription
NameYes<variable-name>The name for the variable to change
ValueYes<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 variablesinside 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 expectsame 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. Drag the Degree of Parallelism slider to 1.

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

PropertyRequiredValueDescription
NameYes<variable-name>The name for the variable to change
ValueYes<append-value>The value you want to append, which can have any type

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

  • Learn about Logic Apps connectors
Create and manage variables for storing and passing values - Azure Logic Apps (2024)

FAQs

How do I create a managed identity for logic app? ›

Before you can enable the user-assigned identity on your Logic App (Consumption) or Logic App (Standard) resource, you have to first create that identity as a separate Azure resource. In the Azure portal search box, enter managed identities . Select Managed Identities. On the Managed Identities pane, select Create.

How do I secure access and data in Azure logic apps? ›

In the Azure portal, open your logic app in the workflow designer. On the trigger or action where you want to secure sensitive data, select the ellipses (...) button, and then select Settings. Turn on either Secure Inputs, Secure Outputs, or both.

How do I create a variable in Azure? ›

Create a variable. You can create variables in your pipeline with the az pipelines variable create command. To get started, see Get started with Azure DevOps CLI.

How do you create a variable in Azure pipeline? ›

You'll use the task. setvariable logging command to set variables in PowerShell and Bash scripts. Deployment jobs use a different syntax for output variables. To learn more about support for output variables in deployment jobs, see Deployment jobs.

What are the three methods to pass parameters to the operating system? ›

Preview text
  • Pass the parameters in registers (this may prove insufficient when there are more parameters than registers).
  • Store the parameters in a block, or table, in memory, and pass the address of block as a parameter in a register. ...
  • Push the parameters onto a stack; to be popped off by the OS.

How do you pass parameters in field values? ›

Pass parameters to set field record values
  1. You must encode the parameters passed in the extraqs parameter. ...
  2. The names of the query string arguments must match or include the names of attributes for the entity.
  3. The values passed must be valid.
  4. The value can't be a script.
Feb 15, 2022

How do you pass a value in a parameter? ›

You can pass values to the parameters that are declared in the AccessProfile widget by reference, by value, or by specifying the direct value. Define this option in the main AccessProfile. This topic provides an example of an AccessProfile widget and a main AccessProfile.

How do you create a managed identity in Azure app Service? ›

An app can have multiple user-assigned identities.
  1. Add a system-assigned identity.
  2. Add a user-assigned identity.
  3. Configure target resource.
  4. Connect to Azure services in app code.
  5. Remove an identity.
  6. REST endpoint reference.
  7. Next steps.
Nov 10, 2022

How do I create a managed identity in Azure? ›

Assign a user-assigned managed identity to an existing VM
  1. Sign in to the Azure portal using an account associated with the Azure subscription that contains the VM.
  2. Navigate to the desired VM and click Identity, User assigned and then +Add.
  3. Click the user-assigned identity you want to add to the VM and then click Add.
Aug 18, 2022

How do I manage my logic apps? ›

Find logic apps

Open Visual Studio. On the View menu, select Cloud Explorer. Next to the Account Management icon, select Resource Types. Under your Azure subscription, expand Logic Apps so that you can view all the deployed logic apps that are associated with your subscription.

How do I grant access to key vault in logic app? ›

In order to configure access policies, navigate to the Key Vault and select Access policies in the left hand menu. Select Add Access Policy to provide access to a new principal. Then you have the option to add an access policy.

How do I connect to Azure SQL database from logic apps? ›

Add a SQL Server action
  1. In the Azure portal, open your logic app workflow in the designer.
  2. Find and select the SQL Server action that you want to use. ...
  3. Provide the information for your connection. ...
  4. If you haven't already provided the SQL server name and database name, provide those values.
Oct 31, 2022

What are the 4 rules for creating variables? ›

A variable name must start with a letter or an underscore character (_) A variable name cannot start with a digit. A variable name can only contain alpha-numeric characters and underscores ( a-z, A-Z , 0-9 , and _ ) Variable names are case-sensitive (age, Age and AGE are three different variables)

How do you create and assign values to variables? ›

Assign values to variables
  1. Use a LET statement.
  2. Use a SELECT INTO statement.
  3. Use a CALL statement with a procedure that has a RETURNING clause.
  4. Use an EXECUTE PROCEDURE INTO or EXECUTE FUNCTION INTO statement.

How do you create a variable and set it to a value? ›

The first time a variable is assigned a value, it is said to be initialised. The = symbol is known as the assignment operator. It is also possible to declare a variable and assign it a value in the same line, so instead of int i and then i = 9 you can write int i = 9 all in one go.

How do you pass variables in Azure CLI? ›

Use shell variables

You can use variables in Bash to pass values for parameters to commands. Using variables with the Azure CLI also allows reuse of commands, either piecemeal or in scripts. This example creates a new storage disk of the same type as the storage disk on an existing virtual machine.

How do you create a variable? ›

To create a variable, you give it a type, a name, and a value.
  1. The type tells Processing what kind of value the variable will hold.
  2. The name is how you'll use the variable later in the code, like you've used width and height .
  3. The value is what the variable points to.

What is the difference between variables and parameters in Azure pipelines? ›

Pipeline variables are values that can be set and modified during a pipeline run. Unlike pipeline parameters, which are defined at the pipeline level and cannot be changed during a pipeline run, pipeline variables can be set and modified within a pipeline using a Set Variable activity.

What are the two most common methods of parameter passing? ›

There are different ways in which parameter data can be passed into and out of methods and functions. It is beyond the scope of these notes to describe all such schemes, so we will consider only the two most common methods used in C++ and Java: "pass by value" and "pass by reference".

What are the two common method for passing parameters? ›

Parameter passing:- The mechanism used to pass parameters to a procedure(subroutine) or function. The most common methods are to pass the value of the actual parameter (call by value), or to pass the address of the memory location where the actual parameter is stored (call by reference).

What are the two different types of parameter passing? ›

A Parameter is the symbolic name for "data" that goes into a function. There are two ways to pass parameters in C: Pass by Value, Pass by Reference.

What are the five methods of parameter passing? ›

6.1. Parameter-Passing Mechanisms
  • Call-by-value.
  • Call-by-reference.
  • Call-by-copy-restore (also known as value-result, copy-in-copy-out)

Why parameters should be passed by value? ›

When a parameter is passed by value, the caller and callee have two independent variables with the same value. If the callee modifies the parameter variable, the effect is not visible to the caller.

When parameters are passed by value? ›

When you use pass-by-value, the compiler copies the value of an argument in a calling function to a corresponding non-pointer or non-reference parameter in the called function definition. The parameter in the called function is initialized with the value of the passed argument.

How do I add a variable in Anylogic? ›

Creating a variable
  1. Drag the Variable element from the Agent palette to the agent diagram.
  2. In the Properties view, type the name of the variable in the Name edit box. ...
  3. Specify the type of the variable. ...
  4. Specify the variable's initial value in the Initial value edit box.

Can you create variables in YAML? ›

To represent the variations among those different systems, you can create variables with standard YAML syntax, including lists and dictionaries. You can define these variables in your playbooks, in your inventory, in re-usable files or roles, or at the command line.

How you declare a variables in pipeline? ›

Variables in a Jenkinsfile can be defined by using the def keyword. Such variables should be defined before the pipeline block starts. When variable is defined, it can be called from the Jenkins declarative pipeline using ${...} syntax.

Top Articles
Latest Posts
Article information

Author: Corie Satterfield

Last Updated:

Views: 6534

Rating: 4.1 / 5 (42 voted)

Reviews: 81% of readers found this page helpful

Author information

Name: Corie Satterfield

Birthday: 1992-08-19

Address: 850 Benjamin Bridge, Dickinsonchester, CO 68572-0542

Phone: +26813599986666

Job: Sales Manager

Hobby: Table tennis, Soapmaking, Flower arranging, amateur radio, Rock climbing, scrapbook, Horseback riding

Introduction: My name is Corie Satterfield, I am a fancy, perfect, spotless, quaint, fantastic, funny, lucky person who loves writing and wants to share my knowledge and understanding with you.