Sep 24, 2020 12:40 AM
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.
Solved! Go to Solution.
Sep 24, 2020 11:37 PM
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,
});
}
Sep 24, 2020 11:37 PM
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,
});
}
Sep 25, 2020 12:10 AM
Absolutely amazing … The scripting block is so cool for people who don’t really know how to code like me !
thank you @JonathanBowen !