Re: Reduce Script Runtime

239 0
Showing results for 
Search instead for 
Did you mean: 
4 - Data Explorer
4 - Data Explorer

I have a script that takes a fuel price increase in a single cell and applies it in each cell in a row of fuel prices at various Fuel Stations. The code is copied below. How can i optimise the code to run in as little time as possible?

let table = base.getTable("Customer Deals");
let queryResult = await table.selectRecordsAsync({fields: ["Petrol Price", "Price Increase"], sorts: [{field: "Price Increase", direction: "desc"}]});
let firstRecord = queryResult.records[0];
let increase = firstRecord.getCellValue("Price Increase");

if (!(increase === 0)) {
    for (let record of queryResult.records) {
        let newPetrolPrice = record.getCellValue("Petrol Price") + increase;
        await table.updateRecordAsync(record, {'Petrol Price': newPetrolPrice});

    await table.updateRecordAsync(firstRecord, {'Price Increase': 0});
1 Reply 1

You are updating records one at a time inside the for loop. 

Instead, build an array of record updates to submit in batches of 50 using table.updateRecordsAsync(). Note the ‘s’ in the function name. See the scripting documentation for more details. This example script in the documentation has an example of doing batch updates in groups of 50.