Skip to main content

01.1.2 Creating a scenario

Creating a Scenario and Adding Nodes

To start automating a function, you need to add a new scenario. All created scenarios are available for viewing and managing on a separate interface. Each scenario has a menu that allows you to, for example, activate the scenario without opening it.

Once a scenario is created, nodes can be added by selecting them from the list. Each added node will require customization, and all nodes must be connected via node connection points.

Nodes specific to certain applications and services also require authorization. Authorization can be set up in the node itself or on the authorizations page by selecting the required service from the list.

As an example, let's create a scenario that analyzes a website's exchange rate page, searches for the desired exchange rate based on specified parameters, writes the found value into a variable, and sends the user an email with the required information.

Passing JSON to a Scenario: Trigger on Webhook Node with Postman/Insomnia

To start any scenario, you must add a trigger node. In our example, we need a Trigger on Webhook node because the scenario requires passing data. The data will consist of two currency names, C1 and C2. The scenario can transfer files or JSON objects. Postman or Insomnia can be used for data transfer. The address of the Trigger on Webhook node should be specified as the request address.

{
"C1": "Hong Kong Dollar",
"C2": "Polish Zloty"
}

Headless Browser Node and Site Parsing

To process a website page, you need to add a Headless Browser node and include code in the node. The result of this node's execution will be the current exchange rate for the specified currencies. The code will process the web page using the data passed to the Trigger on Webhook node:

// Get currency codes from the data transmitted by node 1
const currencyCode1 = data["{{1.\`body\`.\`C1\`}}"]; // Variable for the first currency code
const currencyCode2 = data["{{1.\`body\`.\`C2\`}}"]; // Variable for the second currency code

// Set User-Agent to simulate browser requests
await page.setUserAgent('Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36');

// Go to the page with Bank of England exchange rates
await page.goto('https://www.bankofengland.co.uk/boeapps/database/Rates.asp?Travel=NIxIRx&into=GBP');

// Wait for the table with exchange rates to be loaded
await page.waitForXPath('//*[@id="editorial"]/table');

// Search for the element with the currency rate corresponding to the first code and get its value
const currency1Element = await page.waitForXPath(\`//td[a[contains(@title, "${currencyCode1}")]]/following-sibling::td[1]\`, {timeout: 30000});
var currency1 = await currency1Element.evaluate(el => el.textContent.trim());

// Search for the element with the currency rate corresponding to the second code and get its value
const currency2Element = await page.waitForXPath(\`//td[a[contains(@title, "${currencyCode2}")]]/following-sibling::td[1]\`, {timeout: 30000});
var currency2 = await currency2Element.evaluate(el => el.textContent.trim());

// Build a JSON object with the desired structure
const result = {
C1: \`${currency1}\`,
C2: \`${currency2}\`
};

// Return this object
return result;

To test the nodes, you can launch them individually. This will execute the node with the specified parameters (including known data from previous nodes) and display an information message with the results in the upper right corner of the node.

Creating Global Variables

Often, the data received in nodes needs to be saved. The Latenode platform allows you to save data in two types of variables. The first type, global variables, allows data to be used by other scenarios. The second type, local variables, restricts the use of data to the scenario in which it was obtained.

You can manage variables or global variables using special nodes or JavaScript code in the Headless Browser or JavaScript nodes.

For global variables, an interface for creating and editing variables is also available. Let's create global variables C1 and C2 and set their initial values. You can update these values with actual data later in the scenario.

JavaScript Node and Data Processing with Code

The JavaScript node can be used to process data. For example, you can compare the values obtained from the Headless Browser node with the values of the global variables you created. Calculate the difference and generate a message based on the results. The message text will vary depending on whether the exchange rate has risen or fallen. You can use the following code:

export default async function run({execution_id, input, data, store}) {
// Get global variables
const globalC1 = await store.getGlobalVariable('C1');
const globalC2 = await store.getGlobalVariable('C2');

// Get parameters from the Headless browser node
const nodeC1 = data["{{$2.C1}}"];
const nodeC2 = data["{{$2.C2}}"];

// Get currency names
const nameC1 = data["{{$1.body.C1}}"];
const nameC2 = data["{{$1.body.C2}}"];

// Convert values to numeric format
const globalC1Number = parseFloat(globalC1);
const globalC2Number = parseFloat(globalC2);
const nodeC1Number = parseFloat(nodeC1);
const nodeC2Number = parseFloat(nodeC2);

// Compare the values modulo and calculate the difference between the variable value and the Headless browser node data
const diffGlobalC1NodeC1 = Math.abs(globalC1Number - nodeC1Number).toFixed(4);
const diffGlobalC2NodeC2 = Math.abs(globalC2Number - nodeC2Number).toFixed(4);

let messageC1;

if (globalC1Number - nodeC1Number < 0) {
messageC1 = \`The currency rate of ${nameC1} has increased by ${diffGlobalC1NodeC1} and is ${nodeC1}.\`;
} else if (globalC1Number - nodeC1Number === 0) {
messageC1 = \`The currency rate of ${nameC1} has not changed and is ${nodeC1}.\`;
} else {
messageC1 = \`The currency rate of ${nameC1} has fallen by ${diffGlobalC1NodeC1} and is ${nodeC1}.\`;
}

let messageC2;

if (globalC2Number - nodeC2Number < 0) {
messageC2 = \`The currency rate of ${nameC2} has increased by ${diffGlobalC2NodeC2} and is ${nodeC2}.\`;
} else if (globalC2Number - nodeC2Number === 0) {
messageC2 = \`The currency rate of ${nameC2} has not changed and is ${nodeC2}.\`;
} else {
messageC2 = \`The currency rate of ${nameC2} has fallen by ${diffGlobalC2NodeC2} and is ${nodeC2}.\`;
}

// Separate the sentences with a dot and start the second sentence on a new line
return {
message: messageC1 + " \n" + messageC2
}
}

Saving Data in Global Variables

We previously created two global variables, C1 and C2, which contain exchange rate data. This data can be updated daily, and our scenario involves comparing the new data with the previous values. Therefore, each time we receive an update, we need to save the values in the C1 and C2 variables again. This can be done directly in the scenario using the SetGlobalVariables node. This node allows both the creation of new variables and the overwriting of existing variable values.

Using APP Nodes and Authorization

App nodes can be used to perform specific functions. An example is the Send Mail node, which sends an email to a specified address. Often, using these nodes requires setting up authorization, allowing the Latenode platform to access the necessary functions.