Skip to main content

10.1 Creation and use of variables in the JavaScript node

With the JavaScript node, it is possible to manage simple variables.

Сreate Variables

To create a variable using the JavaScript node, follow these steps:

  1. Add a JavaScript node to the scenario with the following code:
export default async function run({ execution_id, input, data, store }) {
// Set Vars directly from JS
// String are available
const v_str = await store.setVariable("VarFromJs", "var value");

return {
}
}

This code represents an asynchronous function run. This function is designed to be executed in a web automation scenario and utilizes objects  execution_idinput, data, and store.

The function defines the variable VarFromJs and saves it using the store.setVariable method. This variable can only be used within the current scenario.

  1. Run the JavaScript node once and wait for its execution;
  1. Check for the existence of the new variable when populating parameters for any other node.

Getting Variables

To get a variable using the JavaScript node, follow these steps:

  1. Add a JavaScript node to the scenario with the following code:
export default async function run({ execution_id, input, data, store }) {

// Get Vars
const res_v_str = await store.getVariable("VarFromJs")

return {
res_v_str
}
}

This code represents an asynchronous function run. This function is designed to be executed in a web automation scenario and utilizes objects  execution_id,  input,  data, and store.

The code extracts the values of the variable using the store.getVariable method. This method returns the value of a previously set variable.

  1. Run the JavaScript node once and wait for its execution;
  1. Check the output data of the JavaScript node for the variable values:
tip

Before retrieving the variable value, ensure to create the variable. Variable creation can be done using the SetVariables node or the JavaScript node but must occur before obtaining the variable value in the JavaScript node