Dear community,
I have very little experience with scripting and that is why I am struggling with customizing the currency converter example.
My code:
// Change this to the name of a table in your base
let table = base.getTable('GT_MasterData');
// Fetch conversion rate from API - you could change this to any API you want
let apiResponse = await fetch('https://api.exchangerate.host/latest?base=CZK');
let data = await apiResponse.json();
let conversionRate = data.rates.EUR;
output.text(`Kurz: ${conversionRate}`);
// Update all the records
let result = await table.selectRecordsAsync({fields: ['nakupni_cena_eur_ke_konverzi', 'nakupni_cena_po_konverzi']});
for (let record of result.records) {
await table.updateRecordAsync(record, {
// Change these names to fields in your base
'nakupni_cena_po_konverzi': record.getCellValue('nakupni_cena_eur_ke_konverzi') / conversionRate,
});
}
I would like to automate script triggering by time via “Automations”. In order to do that, I need to implement batching to the code above:
// Only up to 50 updates are allowed at one time, so do it in batches
while (updates.length > 0) {
await table.updateRecordsAsync(updates.slice(0, 50));
updates = updates.slice(50);
}
And I am struggling to do that.
Also I have no idea how to implement multiple currency rates: i.e.
field “currency” = “SEK” → then it will use different rates for calculation.
Please help.
Thank you, any help is much appreciated.
-Rob