Airscript Learn to use Airtable scripts

createRecord vs. createRecords (part 1)

One of the actions we’ll come back to again and again as Airtable script developers it creating records in an Airtable table.

The scripting block offers us two handy methods for creating records:

  • creating a single record
  • creating a batch of records (up to 50) in a single pass

This post will be the first in a mini-series that looks at the first of these two approaches.

I’ve set up a base that has an imported set of data - first name and last name:

We want to iterate through these records and create a new record in our “People” table where the key field is “Full Name” - the concatenation of first name and last name.

Our method for this is straightforward. We’ll:

  • define our table and a query on the table
  • we’ll take the query results and iterate through these one by one
  • on each iteration we’ll create a new record in our people table

So our script looks something like this:


let importTable = base.getTable('Imported table');
let importQuery = await importTable.selectRecordsAsync();

let peopleTable = base.getTable('People');

for (let record of importQuery.records) {
    let fullName = record.getCellValue('First Name') + ' ' + record.getCellValue('Last Name');
    await peopleTable.createRecordAsync({
        "Full Name": fullName
    })
    output.text(`New record for ${fullName} created!`);
}

Let’s walk through this script:

  • we start by definining the import table and defining a variable importQuery that is a query on the table
  • we then define our 2nd table, “People”
  • we loop through the records returned by the query with:

for (let record of importQuery.records) {
  // stuff happens here
}

  • and, for each iteration, we join the first name with the last name (separated by a space) and assign this to the variable fullName
  • we create a new record on the “People” table using the method createRecordAsync
  • finally, we output some text to the user to keep them updated on progress

Let’s drill down into the createRecordAsync method. It is preceeded by the await command so that the create action completes before we move onto the next iteration.

We call the create method on a specific table so the method call will take the form:

tableVariable.createRecordAsync()

We pass an object to the method/function (an object defined by key/value pairs within a set of curly braces) that describes the fields we want to assign values to when we create the record, along with the value that we want to set them to. So the object might look something like this:


{
    "Field Name 1": field_name_1_value,
    "Field Name 2": field_name_2_value,
    ...
}

Depending upon the field type that we are assigning values to, the value might be a variable representing a string of text or might be an array of values or even an array of objects.

For completeness, here’s what the “People” table looks like when the script is run:

Recap

  • define the tables and queries
  • loop through the query records
  • create a new record in table 2 on each iteration
  • output some text for a nice user experience