Help

Re: Delete a record by automation

Solved
Jump to Solution
139 0
cancel
Showing results for 
Search instead for 
Did you mean: 
ido
4 - Data Explorer
4 - Data Explorer

I am looking to Run a script in an Action. basically whenever the Category “Done” or “Postpone” is set to the record by single select field. I like to run a script that deletes those records. The trigger is pretty much easy to do by the automation configuration. The script is the one I didn’t figure out how to do.

13 Replies 13

Maybe you did not declare RecorId in the input.config() column?

Alvaro_Hernande_0-1695312958381.png

 



You didn't pass the record ID. Should be done in such way. Table name must be the same as in trigger
variable name ('id') - is that you must choose at the left side of code editor.

Alexey_Gusev_0-1720418653407.png

 



You should also understand that it doesn't remove current 'Done' records, it just affects new. Or you can remove 'Done' status (for many records as well) and then press Ctrl+Z. Status will be returned, but each of records will run automation to be deleted. Don't recommended to run for more than 50 at a time -  it can cause automation run delay and failed runs.

brdemers
5 - Automation Enthusiast
5 - Automation Enthusiast

FOR ANYONE LOOKING FOR A SOLUTION to deleting records for new form submissions. I found a solution on youtube (thanks Dan Leeman) (see screenshot for my flow) 

const table = base.getTable('TABLE NAME');
const recordsToDelete = input.config().recordIds;
table.deleteRecordsAsync(recordsToDelete);

HERE IS THE CATCH 
1.MAKE SURE you have a find record step, or any way to pull the ID you want
2. When you configure the config step the name needs to be 'recordIds' (didnt know that mattered) 

https://www.youtube.com/watch?v=hNsPTMe_pso 

Screenshot 2024-10-28 at 4.10.10 PM.png

Screenshot 2024-10-28 at 4.10.24 PM.png

A few comments on it:
MAKE SURE you have a find record step, or any way to pull the ID you want - Find Records always returns ARRAY of IDs, not a single ID. Even when you set Max number = 1 , it returns ARRAY having 1 element. 
[ 'recABC1223xyxdef' ]. Airtable improved their automations - when it expects a single ID from you , and you put array (with a single value), it auto-converts and uses it as a single value.
Linking / Lookup field value is also always array.
Function deleteRecordsAsync expects ARRAY of IDs as input, so it's OK in this case.

When you configure the config step the name needs to be 'recordIds' (didnt know that mattered) 
You can use any name you want, like 'records_to_be_removed' or 'recs'. The only thing that matters - name XXXX on the left side of the editor window must match name in input.config().XXXX
when you have several variables on a left side, use ES6 syntax to assign them all in one step, like
{firstVar, second_variable, name, id, whatever} = input.config()

Regarding code, the line 

 

 

 

table.deleteRecordsAsync(recordsToDelete);

 

 

 

won't work for records number>50.
also, in order to proper asynchronous function work, you should run it with await.
without await, your example still working 'as is', but can cause problem being part of bigger solution.
correct call is 

 

 

 

await table.deleteRecordsAsync(recordsToDelete)

 

 

 

when your recordsToDelete length is > 50, you have to run in batches:

 

 

 

while (recordsToDelete.length > 0) {
    await peopleTable.deleteRecordsAsync(recordsToDelete.slice(0, 50));
    recordsToDelete = recordsToDelete.slice(50);
}

 

 

 

or, using 'more eco-friendly' splice, that cuts part of existing array and not 'throwing it away' but returns it as function output, and the fact about (recordsToDelete.length > 0) that ( 0 ) = false,  (not 0) = true, it can be written as:

 

 

 

while (recordsToDelete.length) await peopleTable.deleteRecordsAsync(recordsToDelete.splice(0, 50));

 

 

 

so, finally it can be written as

 

 

 

const table = base.getTable('TABLE NAME');
const recordsToDelete = input.config().recordIds;
while (recordsToDelete.length) await table.deleteRecordsAsync(recordsToDelete.splice(0,50))

 

 

 

 

it's simple and clear but sometimes, when you are sure their number always below 50, you can use one-liner

 

 

 

await base.getTable('TABLE NAME').deleteRecordsAsync(input.config().recordIds)