Retrieving Currency Exchange Rate Data in Tabular format
Script Source (Table)
First, in the Script Source (Table) node, we retrieve data from an external API and return it in tabular format to the following node.
var request =HttpUtil.get("https://boi.org.il/PublicApi/GetExchangeRates");var jsonContent =JsonUtil.parse(request.asString().getBody());var exchanges =jsonContent.exchangeRates;var table =SuiteTable.create("MyTable"); // create tableSuiteTable.addStringColumn(table,"key"); // add columnsSuiteTable.addDoubleColumn(table,"rate");SuiteTable.addStringColumn(table,"lastUpdate");for (var i =0; i <exchanges.size(); i++) { // iterate over array and add Rowvar item = exchanges[i];var key =item.key.asText();var rate =item.currentExchangeRate.asDouble();var date =item.lastUpdate.asText();var row = { "key": key,"rate": rate,"lastUpdate": date };SuiteTable.addRow(table, row);}return table;
Custom Script
Next, we can connect the source script node to custom script node. It receives the table and returns it as it is. In the node itself though, we perform some calculations to set some properties for future use.
// Get a copy of the table reference from the previous nodevar table =NodeInputReader.inputAsDataFrame();//Get a new table sorted by the second column descendant (highest to lowest rates)table =table.sortOn(-1);//Get the values of the second column from the first row (Index 0, highest rate)var maxcurr =table.column(0).get(0); // keyvar maxvalue =table.column(1).get(0); // rateProperty.global.set("maxcurr", maxcurr); // max currency nameProperty.global.set("maxvalue", maxvalue); // max currency valuereturn table;
Under properties tab in the Custom Script Node we create the following properties:
MAXCURR
Property.global.get("maxcurr");
MAXVALUE
Property.global.get("maxvalue");
LOCALDATE
LocalDate.now()
USD
var table =NodeInputReader.inputAsDataFrame(); //Get the value of the first column from the first row (USD rate) var maxValue =table.column(1).get(0);
EUR
var table =NodeInputReader.inputAsDataFrame(); //Get the values of the 4th column from the first row (EUR rate) var maxValue =table.column(1).get(3);
Data Prep
Next, the custom script node sends the table to data prep node in tabular format.
Unfortunately, the table we received contains dates with terrible format such as
2023-11-20T13:23:13.8650642Z.
We can use custom column to write a script for a new column with better date format.
// Parse the datetime string into an Instantvar instant =Instant.parse(lastupdate);// Get the year, month and day components from the Instantvar year =InstantUtil.getYear(instant);var month =InstantUtil.getMonth(instant);var day =InstantUtil.getDayOfMonth(instant);// Create a LocalDate object using the extracted year, month, and dayvar localDate =LocalDate.of(year, month, day);
Now we have a new date column which displays the date in a clearer format.
Excel Builder
Next, we can connect the data prep node to excel builder node to get the table in excel format.
Send Email
Lastly, we can use the Send Email node to send an email at the end of our workflow with the excel file as an attachment and the following body:
${DATE} currency exchange rates:USD: ${USD}EUR: ${EUR}Max is ${MAXCURR} at ${MAXVALUE}Have great day!
Here we used the properties we defined earlier as global properties:
DATE, USD, EUR, MAXCURR, and MAXVALUE.
Extra: Scheduler
We can also set a scheduler to run this workflow on a daily basis and receive this email with up-to-date info about currency exchange rates with the following CRON expression