Hello all,
I know this looping question has been posed in various forms over the years, but I could use a gentle nudge in the right direction as my research has reached an impasse.
I have two tables - the 'primary' table has ~1200 records, and the 2nd table has ~800 records. I'd like to create a script to compare the two tables and update field XXL2 in the primary table with the string 'XXX' if a matching record in the 2nd table matches a field in the primary table.
I have seen that I need to populate an array with the 'found' results and then cycle through updating the results using slices of 50, but I know there is more to it to ensure that the code completes within the 30-second timeout.
What I have thus far is:
let uncontractedComponentsTable = base.getTable("Uncontracted Components Report");
let vsaCancellationsTable = base.getTable("VSA Cancellations");
let updates = [];
// grab the records from the Uncontracted Components Report table
let uncontractedComponentsRecords = await uncontractedComponentsTable.selectRecordsAsync();
// grab the records from the VSA Cancellations table
let vsaCancellationsRecords = await vsaCancellationsTable.selectRecordsAsync();
// loop through each record in the VSA table
for (let vsaRecord of vsaCancellationsRecords.records) {
let cancelledTour = vsaRecord.getCellValue("CancelledTour");
// loop through each record in the Uncontracted Comp table
for (let uncontractedRecord of uncontractedComponentsRecords.records) {
let record = uncontractedRecord.id;
let depCode = uncontractedRecord.getCellValue("Dep. Code");
// if the fields match, update the array with the recordID and XXX
if (cancelledTour === depCode) {
updates.push({
id: record,
fields: { "XXL2": "XXX"}
})
}
}
while (updates.length > 0) {
await vsaCancellationsTable.updateRecordsAsync(updates.slice(0, 50));
updates = updates.slice(50)
}
}
In a previous iteration, it worked when I limited the records in the 2nd table (VSA Cancellations) to just one or two.
The above code now throws an error "Could not find a record with ID 'xxxxxxx' " .... yet the ID does exist.
Can anyone point me in the right direction, please?