Help

Save the date! Join us on October 16 for our Product Ops launch event. Register here.

Currency conversion - batching

Topic Labels: Scripting extentions
1001 2
cancel
Showing results for 
Search instead for 
Did you mean: 
Robert_Vagner
4 - Data Explorer
4 - Data Explorer

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

2 Replies 2

Welcome to the Airtable community!

Two different approaches to your goal come to mind.

The first approach is to hack at things in your script until it looks more like the script from which you got the batch processing code.

The second approach is to get familiar with working with arrays in JavaScript, possibly by taking a course in JavaScript, then examining the batch processing script until you understand how the array and the batch process works. Then, apply the same concepts to your script.

If you need to get the rate for different currencies, also keep in mind that automation scripts have a limit of 50 fetch requests, and the entire script needs to finish in 30 seconds.

Robert_Vagner
4 - Data Explorer
4 - Data Explorer

Thank you Kuovonne! Until I take a course of JavaScript, anyone with experience to help with the first approach?