Skip to main content
Quick navigation

Set Variable Task

The Set Variable task enables you to assign values to a local variable for use in your Test Scenario.

You can define a value explicitly, or reference data from a chained task using the Task Ports.

Unlike environment variables, which can be used globally across any scenario, local variables are only available within the context of the scenario they are set.

Create a Set Variable task

When inside the visual editor for a new scenario, select the Scenario Start button and select Set Variables from the dropdown menu.

Enter the variable value in the Value input, and add your variable name in the Assign to input.

Note you can switch between the different Custom Inputs for assigning values to variables using different methods. For example, via a JavaScript snippet.

Accessing a local variable

Once you have configured a local variable, it's possible to access it in subsequent tasks.

Depending on which custom input you are using, you should access local variables differently.

AttributeHow to access: Field selection (JQ)How to access: JavaScript
Local variable.variables.productIdcontext.variables.productId

Chain a producer task from the VARIABLES port of the previously configured task.

Navigate to the Data tab of your Producer task.

To utilize the variable as the message value, use the Template (mustache) input type.

Click into the Value field, and use the helper to select from a list of available variables.

Accessing data from a previous task

it's possible to set local variables from data relevant to the context of a previous task. You can do this by utilising the triggeredBy property.

For example, if you have a Set Variable task that's chained to the ON RECORD port of a Consumer task, you can access the record key and value.

AttributeHow to access: Field selection (JQ)How to access: JavaScript
Record Key.triggeredBy[0].record.keycontext.triggeredBy[0].record.key
Record Value.triggeredBy[0].record.valuecontext.triggeredBy[0].record.value

Example: Setting a variable from a JSON message value via a Consumer task

Note you should use the Field Selection (JQ) custom input to access the data

If you have more than one origin for a task, you can use Field Selection (JQ) or Javascript advanced filtering options to pick the right one.