Help

The Community will be temporarily unavailable starting on Friday February 28. We’ll be back as soon as we can! To learn more, check out our Announcements blog post.

Deleting row with button script in a task list

Topic Labels: Scripting extentions
Solved
Jump to Solution
1898 1
cancel
Showing results for 
Search instead for 
Did you mean: 
Jesper_Holmstro
6 - Interface Innovator
6 - Interface Innovator

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!”);

1 Solution

Accepted Solutions
Justin_Barrett
18 - Pluto
18 - Pluto

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: </>).

See Solution in Thread

1 Reply 1
Justin_Barrett
18 - Pluto
18 - Pluto

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: </>).