Apr 04, 2021 07:59 AM
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.
Solved! Go to Solution.
Sep 21, 2023 09:16 AM
Maybe you did not declare RecorId in the input.config() column?
Jul 07, 2024 11:04 PM
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.
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.
Oct 28, 2024 01:10 PM
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)
https://www.youtube.com/watch?v=hNsPTMe_pso
Oct 28, 2024 02:39 PM - edited Oct 28, 2024 03:03 PM
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)