Help

Re: How can I only update empty fields in a table?

1520 0
cancel
Showing results for 
Search instead for 
Did you mean: 
Vincent_van_der
4 - Data Explorer
4 - Data Explorer

Hi everyone!

I have altered the example script a bit to update the ‘£ Rate’ field on all records in my ‘Cash Input’ table. I want the script to update the ‘£ Rate’ field only in new records that do not already have a value in the ‘£ Rate’ field. How can I do that? What changes does my code need?

I use this code:

let table = base.getTable('Cash Input');
// Fetch conversion rate from API - you could change this to any API you want
let apiResponse = await fetch('ttps://api.exchangeratesapi.io/latest?base=EUR');
let data = await apiResponse.json();
let conversionRate = data.rates.GBP;
output.text(`Conversion rate: ${conversionRate}`);
// Update all the records
let result = await table.selectRecordsAsync();
for (let record of result.records) {
    await table.updateRecordAsync(record, {
        // Change these names to fields in your base
        '£ Rate':conversionRate,

    });

}
3 Replies 3

Hi!

You can add an if to only update records when the cell is empty (null😞

// Change this to the name of the field your base
const rateFieldName = '£ Rate';

for (let record of result.records) {
    // Only update the record if the cell is empty:
    if (record.getCellValue('£ Rate') === null) {

        await table.updateRecordAsync(record, {
            '£ Rate': conversionRate,
        });

    }
}

Hi Kasra,

Thank you so much for the quick reply. I had tried something like that but without succes. Im such a noob. I use this table to imput cash that is send to pay for weborders. It calculates the value in Eur and checks it vs the orderamount from the website. Im setting up zapier and webhooks now to automatically change the orderstatus on the website. Ill add your code now.

Have a great day!

Vincent

peter_brobeck
5 - Automation Enthusiast
5 - Automation Enthusiast

this the way but this is not working actually…very annoying to condition on cellValues