Aug 10, 2023 05:54 PM
Hello!
I have an automation set up that when a record enters a view, a script is run to delete the record. Almost every time it runs it throws some failure notices and I get multiple notifications from Airtable. But, it seems to work though. All records are deleted from the view when it runs. Usually there are only 20ish records that enter the view at a single time. And if I watch the view the records will disappear usually in under 10 seconds. Yet, I still receive the failure notices.
Here is the script.
//Identify the table you'd like to delete records from
let table = base.getTable("🗹 Checklists");
console.log(table)
//Identify the view you'd like to delete records from
let view = table.getView("⚙️🅰️ Delete Records View");
console.log(view)
//Select the records that you wish to delete
let recordQuery = await view.selectRecordsAsync();
let recordsToDelete = recordQuery.records;
console.log(recordQuery.records)
//Delete records
for (let deleteRecord of recordsToDelete){
await table.deleteRecordAsync(deleteRecord.id);
}
Here is an example error message.
Any ideas on this would be greatly appreciated!
Solved! Go to Solution.
Aug 10, 2023 08:05 PM
I guess the reason is that Automation is started every time a record is added to the View, so if there are consecutive additions to the View, the record that is listed the first time and has not yet been deleted is also listed in the second Find records.
There needs to be some kind of delaying process.
These discussions would be helpful
Solved: Delay email automation - Airtable Community
Solved: Pause or Delay in Automations - Airtable Community
Aug 10, 2023 08:05 PM
I guess the reason is that Automation is started every time a record is added to the View, so if there are consecutive additions to the View, the record that is listed the first time and has not yet been deleted is also listed in the second Find records.
There needs to be some kind of delaying process.
These discussions would be helpful
Solved: Delay email automation - Airtable Community
Solved: Pause or Delay in Automations - Airtable Community
Aug 10, 2023 10:17 PM
That was it! I had the script below in another automation that adds a delay and I added it to this automation and it worked. Thank you so much!!
function delay(ms) {
var limit = new Date();
limit = limit.setMilliseconds(limit.getMilliseconds() + ms);
while ((new Date()) < limit) {
// do nothing
;
}
}
delay(20000); //delay 20 seconds
Aug 11, 2023 07:28 AM
What does the final Script look like if you don't mind sharing? I could use something like this in my own base.
Aug 11, 2023 08:00 AM
I've run into this problem before and after talking to Airtable support, their solution was using the "When a record matches conditions" trigger – you can use the the same conditions as the view filter and there's no coding required. Plus, it avoids the pitfall of someone modifying the view or something else causing a large number of records popping into (and then out of) the view.
Aug 11, 2023 05:25 PM
See below for screenshots of what the final automation looks like. But keep reading to see another solution!
Another way this could be done is with the automation below. Someone mentioned to me that the previous way I was running it would count as an automation run for each record. The method below will delete all records in the view as on automation run. And now I have it set to run once a week instead of each time a record enters the view.
Here is the text of the script so you can copy and paste.
let table = base.getTable("🗹 Checklists");
let inputConfig = input.config();
let recordId = inputConfig['recordId']
await table.deleteRecordAsync(recordId);
I hope this helps!
Aug 11, 2023 05:26 PM
Thank you for the suggestion!
Aug 12, 2023 01:07 AM
This can also be done in a completely no-code way — that doesn’t require any scripting nor coding — by using Make.
It is as simple as either of the 2 screenshots below.
And if you need it to trigger immediately instead of on a schedule, it can be done with a custom webhook that can be triggered instantly with a button or a URL field in Airtable (or with a script as this thread explains).
Just replace either of the first modules below with a webhook module instead.
Aug 12, 2023 09:24 AM
Thank you for the suggestion!