Deleting row with button script in a task list

Hey everyone
Having a bit of problems with this script.
I have a tasklist and at the end of the record I have a delete button.
When I press the button I want it to delete just that record. But when I use this script it seams to delete just any of the records in the list except the record I want to delete. Can someone pls advise?
// Delete a record in the Tasks table

let table = base.getTable(“Tasks”);

let query = await table.selectRecordsAsync({fields: });

let recordId = query.records[0].id;

await table.deleteRecordAsync(recordId);

console.log(“Deleted a record!”);

The query that is returned on the third line contains all records in the table, but not necessarily in a predictable order. Deleting the first record in that array won’t necessarily mean that you’re deleting the record where you clicked the button.

The way to capture the specific record where a button was clicked is by using input.recordAsync(). Here’s your script modified to use that:

// Delete a record in the Tasks table
let table = base.getTable("Tasks");
let clickedRecord = await input.recordAsync("Pick a record", table);
await table.deleteRecordAsync(clickedRecord);
console.log("Deleted a record!");

If you run the script manually, you’ll be prompted to choose a record. However, clicking a button in a button field that targets this script will bypass the prompt and assign that record to the listed variable (clickedRecord in this case).

For future reference, I recommend styling any code/formula samples that you want to share with the preformatted text styling option in the post editor (the toolbar button looks like this: </>).

2 Likes

This topic was solved and automatically closed 3 days after the last reply. New replies are no longer allowed.