Help

Upcoming database upgrades. Airtable functionality will be reduced for ~15 minutes at 06:00 UTC on Feb. 4 / 10:00 pm PT on Feb. 3. Learn more here

Syntax error in Currency Conversion Scripting by record's date

Topic Labels: Scripting extentions
Solved
Jump to Solution
474 2
cancel
Showing results for 
Search instead for 
Did you mean: 

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

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

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 !