Help

Re: Error when running script to delete records in a view using an automation

Solved
Jump to Solution
2561 1
cancel
Showing results for 
Search instead for 
Did you mean: 
Jess_Helmstette
6 - Interface Innovator
6 - Interface Innovator

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.

error.jpg

Any ideas on this would be greatly appreciated!

1 Solution

Accepted Solutions
Sho
11 - Venus
11 - Venus

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

See Solution in Thread

8 Replies 8
Sho
11 - Venus
11 - Venus

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

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

 

 

DABSNIGHTLY
4 - Data Explorer
4 - Data Explorer

What does the final Script look like if you don't mind sharing? I could use something like this in my own base.

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.

See below for screenshots of what the final automation looks like. But keep reading to see another solution!

1.jpg

 

2.jpg

 

3.jpg

 

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.

4.jpg

 

5.jpg

 

6.jpg

 

7.jpg

 

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!

Thank you for the suggestion!

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.

IMG_0216.jpegIMG_0217.jpeg

Thank you for the suggestion!