Hi @matt_stewart - I had exactly the same issue - tasks and dates. It can be done. Here’s the script I used. On its own, it won’t mean much as it works with a specific base set up, but hopefully you can get a sense of what it is doing and copy the structure:
let tasksTbl = base.getTable('Tasks');
output.text('Updating project dates - please wait');
let status = 1;
while (status > 0) {
status = 0
let tasks = await tasksTbl.selectRecordsAsync();
for (let record of tasks.records) {
let actualEndDate = record.getCellValue('Actual end date');
let actualEndDateCopy = record.getCellValue('Actual end date copy');
if (actualEndDate != actualEndDateCopy) {
status ++;
}
}
// console.log(status)
for (let record of tasks.records) {
let actualEndDate = record.getCellValue('Actual end date');
let actualEndDateCopy = record.getCellValue('Actual end date copy');
if(actualEndDate != actualEndDateCopy) {
await tasksTbl.updateRecordAsync(record, {
"Actual end date copy": actualEndDate
})
}
}
}
output.text('Project dates updated');
There’s a couple of trick in this.
- Use a
while loop and set some check that determines if it should run again
- Query the records inside the loop so that you get the latest records based on updates from previous iterations:
while (status > 0) {
status = 0
let tasks = await tasksTbl.selectRecordsAsync();
note that the selectRecordsAsync is inside the while loop.
The structure I used was:
- Set the checker (status) = 1 to ensure it runs once
- Reset the checker to 0
- query the records and find out if the script has anything to do and set the checker to something other than zero (in my case I was counting how many dates I would need to amend)
- If it does, then do the thing
At this point because my checker is non-zero, it will run again, setting the checker back to zero, getting the records again and so on.
If at any point the line status++ returns zero, then there’s nothing to do and the while loop will finish.
I can see some improvements in the script, so maybe don’t take it verbatim, but hopefully this helps some. You can also use a do-while loop which has at least one run built into it.
More here on that