JS Scripting

Adding a JavaScript code block step to your automations

While Bindings in automations support JavaScript, you can also add a step to execute a JavaScript code block.

Commonly this action is used to compare and aggregate data from multiple Datasources.


Adding a JavaScript code block

  1. After adding an automation Trigger, click the + icon and select JS Scripting. Click Save.

  1. Click the Edit Code button to open the code editor modal.

📘

Make sure to return a value at the end of your script!

  1. Available bindings can be seen on the far right of your modal. These can be clicked to references within your JS scripting area.
Example output

Example output


Tutorial: Linking an API response to table rows

In this tutorial we will look at using a JS Scripting block to link people from an API response to people with the same name in a Budibase table.

  1. Add a REST Query. This example will pull back the Top 100 Birders for a given date. Be sure to Save the query after Send.

  1. We will also add a Budibase DB table for the members of our mock bird club, which will include some additional information not provided by the API, such as Twitter Username.

  1. In the Automate tab, add an automation. This automation will fetch data from both sources and send a summary Email. I will use an App action trigger, but any other trigger could be used.
Fetch the Top 100 birders from the API, and then all our club members

Fetch the Top 100 birders from the API, and then all our club members

  1. Add a JS Scripting block, and Edit Code. Enter the following:
/*
* Compare and combine the local club members that match, by name, with the Top 100 birders from the API.
*/
const top100 = steps[1].response;
const clubMembers = steps[2].rows;

let topClubMembers = top100.filter(row => clubMembers.map(tableRow => tableRow.Name).includes(row.userDisplayName));

return topClubMembers.map(member => ({
	...member,
	...clubMembers.find(tableRow => tableRow.Name === member.userDisplayName)
}));
  1. Finally add the Send Email step. Fill in the details and format the HTML Contents as desired.
let listItems = ""
for (let member of $("steps.3.value")) {
	listItems += `<li>${member.Name} <b>@${member["Twitter Username"]}</b> 

<br />Spotted ${member.numSpecies} species!</li><br/>`
}

return `Congrats to our top bird watchers - give them a shout out on Twitter!

<p><ol>${listItems}</ol></p>`
  1. Click Run test at the top of your automation to see the result!