Help

Syntax error in Currency Conversion Scripting by record's date

Topic Labels: Scripting extentions
Solved
Jump to Solution
975 2
cancel
Showing results for 
Search instead for 
Did you mean: 
tyloria
4 - Data Explorer
4 - Data Explorer

Hello guys, I have an Airtable managing my bills in multiple currencies.

I’ve been trying to customize the [Airtable Currency Converter]
(Airtable Scripting) to convert each record with the conversation rate of its date. I want it to be automated given that I have 5,000 records of bills.

I suspect my syntax is wrong, but maybe the logic of my code is completely wrong too.

My code is the following :

// load table and records

let table = base.getTable("Table 1");

let result = await table.selectRecordsAsync();

// launch a 'for' loop to fetch conversation rate then update each record depending on value of "Date" field

for (let record of result.records) {

    await

    // fetching exchange rate

    let apiResponse = await fetch(`https://api.exchangeratesapi.io/${record.getCellValue("Date")}?base=EUR`);

    let data = await apiResponse.json();

    let conversionRate = data.rates.GBP;

    // updating the record

    table.updateRecordAsync(record, {

        'Exchange rate': conversionRate,

    });

}

The code doesn’t work. Thank you for your help / insights / corrections.

1 Solution

Accepted Solutions
JonathanBowen
13 - Mars
13 - Mars

Hi @tyloria - you’re pretty close. You’ve got a rogue await in the for loop that is causing the issue. This works:

let table = base.getTable('Table 1');

let query = await table.selectRecordsAsync();

for (let record of query.records) {
    let date = record.getCellValue('Date');
    console.log(date);
    let apiResponse = await fetch(`https://api.exchangeratesapi.io/${date}?base=EUR`);
    let data = await apiResponse.json();
    let conversionRate = data.rates.GBP;
    console.log(conversionRate);    
    table.updateRecordAsync(record, {
        'Exchange rate': conversionRate,
    });    
}

See Solution in Thread

2 Replies 2
JonathanBowen
13 - Mars
13 - Mars

Hi @tyloria - you’re pretty close. You’ve got a rogue await in the for loop that is causing the issue. This works:

let table = base.getTable('Table 1');

let query = await table.selectRecordsAsync();

for (let record of query.records) {
    let date = record.getCellValue('Date');
    console.log(date);
    let apiResponse = await fetch(`https://api.exchangeratesapi.io/${date}?base=EUR`);
    let data = await apiResponse.json();
    let conversionRate = data.rates.GBP;
    console.log(conversionRate);    
    table.updateRecordAsync(record, {
        'Exchange rate': conversionRate,
    });    
}

Absolutely amazing … The scripting block is so cool for people who don’t really know how to code like me !

thank you @JonathanBowen !