Set variables

To set a mutable workflow variable, use the Set variable action. You can use this action to declare, update, and access variables throughout your workflow, which allows you to perform more complex workflow operations. For example:

  • Handling API pagination: API requests sometimes require you to keep track of a page token or offset.
  • Handling lists: You can use a variable to initialize an array and perform actions like map and reduce.
  • Iteration: Variables allow you to manipulate and store data inside a for loop. You can then use that data in the rest of the workflow.

Set a variable

To set a variable:

  1. Click the plus (+) icon on your workflow canvas to open the action catalog.
  2. Search for and select the Set variable step.
  3. Click the Set variable step and enter a Step name.
  4. Enter a variable name. Variable names must start with a letter and can contain only alphanumeric characters and underscores.
  5. Enter a value for the variable.
    • Type {{ if you want to use a workflow context variable.
    • To create an object, click the Create object button.
    • To create an array, click the Create array [ ] button.

If you need to change the value of a variable after setting it, you must add an additional Set variable step and either reassign the variable or create a new variable.

Here is an example of a workflow that demonstrates the Set variable step:

  1. In your workflow, start with a Set variable step to declare a variable called intList and give it the value [1,2,3,4].
  2. Add a data transformation expression step with the expression $.Variables.intList.filter(number => number % 2 === 0) to filter out the odd numbers.
  3. Use another Set variable step to assign the result from the expression ([2,4]) to a new variable named evenList.
  4. Add an Echo step to echo the value of evenList.
This workflow sets a variable, performs an Expression step on it, and then sets a new variable with the result of the expression. Finally, it echoes the result of the final variable.

Access a variable

You can access a variable in your workflow using {{ Variables.variableName }}. For example, to access a variable named DashboardList, use {{ Variables.DashboardList }}.

Iteration

Setting a variable inside a For loop allows you store data for use outside of the loop. For example, if you’re making multiple API requests inside a For loop, you can set a variable and append the data you need to it on each iteration. Outside of the loop, you can access the variable and handle the data you collected.

To avoid a type error resulting from an undefined variable, assign a variable before you use it in a loop. In the example below, the variable evenList is set to an empty array before it is used in the loop.

This workflow sets a variable before it is used in a loop.

Further reading

Additional helpful documentation, links, and articles:


Do you have questions or feedback? Join the #workflows channel on the Datadog Community Slack.