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:

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:

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:


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

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