Skip to main content

Reduce Script Runtime

  • December 6, 2023
  • 1 reply
  • 11 views

Forum|alt.badge.img

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

kuovonne
Forum|alt.badge.img+29
  • Brainy
  • December 6, 2023

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.