Help

The Community will be temporarily unavailable starting on Friday February 28. We’ll be back as soon as we can! To learn more, check out our Announcements blog post.

Script exceeded execution time limit of 30 seconds

Topic Labels: Automations
1145 1
cancel
Showing results for 
Search instead for 
Did you mean: 
lukepierce
4 - Data Explorer
4 - Data Explorer

Hello,

When I run the script below as an automation, I get an error:

Script exceeded execution time limit of 30 seconds

 Here is the code:

 

let table = base.getTable("WPS-EVRI");
 
let result = await table.selectRecordsAsync();
 
let sums = {};
 
for (let record of result.records) {
 
let customerAndWeek = record.getCellValue("Customer & Week");
 
if (!sums[customerAndWeek]) {
sums[customerAndWeek] = 0;
}
 
sums[customerAndWeek] += record.getCellValue("DA Amount");
}
 
let updated = {};
 
for (let record of result.records) {
 
let customerAndWeek = record.getCellValue("Customer & Week");
 
if (!updated[customerAndWeek]) {
 
let matchingRecords = result.records.filter(r => r.getCellValue("Customer & Week") === customerAndWeek);
 
for (let matchingRecord of matchingRecords) {
await table.updateRecordAsync(matchingRecord, {
"Total This Week (DA)": sums[customerAndWeek]
});
}
 
updated[customerAndWeek] = true;
}
}

 

 

Is there a way to edit this script so it shortens the timeframe of running?

 

Any help is greatly appreciated! 🙂 

1 Reply 1
Andy_Lin1
9 - Sun
9 - Sun

The most obvious optimization is to change the table.updateRecordAsync() to table.updateRecordsAsync() (note that it can only process 50 records at a time). Where you currently update the record, instead push the update to an array, and then update the records in a batch outside of the loop. Even though the operations are supposed to be asynchronous and can run concurrently, if you're updating a large number of records, the run time can add up.

You can additionally optimize the code by:

  • only requesting the two fields you access with table.selectRecordsAsync({fields: ['DA Amount','Customer & Week']}).
  • assigning 'Customer & Week' to the result.records array objects so you don't have to call getCellValue three times in the script
  • for the "updated" block of code, you can iterate directly using sum.forEach((value, index) => {...}) (you've already created an array where the index itself is a list of unique customerAndWeek values, so you don't really need the 'updated' variable to keep track of what has been updated)