We saw in a previous post that scripts can accept some input from the user. Airtable has a number of input records and one of these is the input.record method, which allows the user to pick a record and run the script against this record (with some element of the record data being used in the script).

Airtable recently introduced the ability to add a button to the grid UI to allow a script to be run using the record the button was attached to, so providing a shortcut to the input.record method we used before. (The button can also be used to complete other actions, such as opening a URL or sending an email via Sendgrid).

I this post I’ll show how to set this up and will give some examples of how this might be used.

Let’s start with a simple table with a single field “Name”:

Add a script block to your base and add this script:


let table = base.getTable("Table 1");
let record = await input.recordAsync('Pick a record', table);
if (record) {
    output.text(`You picked ${record.getCellValueAsString("Name")}`);
}

This is boilerplate code provided by the Airtable script help docs and does a few things:

We can now add a button field to our base:

The configuration of the button is straightforward. We need to set-up:

Now click on a button to run the script. Our script doesn’t do very much at this point - you should see output similar to this:

Now that we’ve got a basic button set up, how can we use it in a real life scenario? The code above provides a basic template and can be modified to do whatever you want to do:


let table = base.getTable("Table 1");
let record = await input.recordAsync('Pick a record', table);
if (record) {
    // your code goes here
}

Airtable recently held a competition to showcase the use of script buttons. You can see my entry here which describes a “holiday request and approval” base. The base has script buttons to approve or deny holiday requests and a script to check if a holiday request overlaps with an existing approved holiday.

It is worth spending some time brwosing through all of the entries in the competition post - there are some great ideas and implementations for button scripts.