Blog
June 19, 2021
How To Insert records through Airtable Scripting
Blog
June 19, 2021
How To Insert records through Airtable Scripting
Inserting records using the Airtable Scripting extension
Inserting Records in Airtable Scripting
Before we get started, make sure you’ve installed the Scripting app in your Airtable base as explained in in Airtable's support articles. To insert records, you will need to copy and paste the following code into your Scripting app. For this example, we assume that your table has at least one field called "Name."
Code Snippet
let table = base.getTable('Teachers');
let recordIds = await table.createRecordsAsync([
{ fields: { 'Name': 'Alice' } },
{ fields: { 'Name': 'Bob' } },
{ fields: {} }
]);
Understanding the Code
In the code above, the first step is to configure the table in which we want to insert new records—in this case, the Teachers table. Next, we use the createRecordsAsync()
function to insert multiple records into the table. Airtable provides two functions for creating records:
createRecordAsync()
– Used to create a single record.createRecordsAsync()
– Used to create multiple records at once.
In this example, we use createRecordsAsync()
to insert an array of records. As shown, the first two records include names, but the third record is empty. This will result in an empty row in the table where all the cells remain unfilled.
Looping Through and Creating Multiple Records
To insert multiple records efficiently, you can use a loop. Here’s an example of how you can loop through and insert records in batches of 50, which is considered good practice to prevent errors or performance issues when working with large datasets:
Code Snippet
while (update_array.length > 0) {
await table.createRecordsAsync(update_array.slice(0, 50));
update_array = update_array.slice(50);
}
In this code, we take the first 50 records from the update_array
and submit them for creation using createRecordsAsync()
. We then remove these records from the array and repeat the process. This method ensures that you are processing records in manageable chunks, improving performance and reliability.
Key Points to Remember
createRecordAsync()
is used for inserting a single record, whilecreateRecordsAsync()
is for inserting multiple records at once.The array of records must follow a specific format, as demonstrated.
If you pass an empty object into the
fields
, it will create a record with empty values.
Inserting Records in Airtable Scripting
Before we get started, make sure you’ve installed the Scripting app in your Airtable base as explained in in Airtable's support articles. To insert records, you will need to copy and paste the following code into your Scripting app. For this example, we assume that your table has at least one field called "Name."
Code Snippet
let table = base.getTable('Teachers');
let recordIds = await table.createRecordsAsync([
{ fields: { 'Name': 'Alice' } },
{ fields: { 'Name': 'Bob' } },
{ fields: {} }
]);
Understanding the Code
In the code above, the first step is to configure the table in which we want to insert new records—in this case, the Teachers table. Next, we use the createRecordsAsync()
function to insert multiple records into the table. Airtable provides two functions for creating records:
createRecordAsync()
– Used to create a single record.createRecordsAsync()
– Used to create multiple records at once.
In this example, we use createRecordsAsync()
to insert an array of records. As shown, the first two records include names, but the third record is empty. This will result in an empty row in the table where all the cells remain unfilled.
Looping Through and Creating Multiple Records
To insert multiple records efficiently, you can use a loop. Here’s an example of how you can loop through and insert records in batches of 50, which is considered good practice to prevent errors or performance issues when working with large datasets:
Code Snippet
while (update_array.length > 0) {
await table.createRecordsAsync(update_array.slice(0, 50));
update_array = update_array.slice(50);
}
In this code, we take the first 50 records from the update_array
and submit them for creation using createRecordsAsync()
. We then remove these records from the array and repeat the process. This method ensures that you are processing records in manageable chunks, improving performance and reliability.
Key Points to Remember
createRecordAsync()
is used for inserting a single record, whilecreateRecordsAsync()
is for inserting multiple records at once.The array of records must follow a specific format, as demonstrated.
If you pass an empty object into the
fields
, it will create a record with empty values.
Inserting records using the Airtable Scripting extension
Inserting Records in Airtable Scripting
Before we get started, make sure you’ve installed the Scripting app in your Airtable base as explained in in Airtable's support articles. To insert records, you will need to copy and paste the following code into your Scripting app. For this example, we assume that your table has at least one field called "Name."
Code Snippet
let table = base.getTable('Teachers');
let recordIds = await table.createRecordsAsync([
{ fields: { 'Name': 'Alice' } },
{ fields: { 'Name': 'Bob' } },
{ fields: {} }
]);
Understanding the Code
In the code above, the first step is to configure the table in which we want to insert new records—in this case, the Teachers table. Next, we use the createRecordsAsync()
function to insert multiple records into the table. Airtable provides two functions for creating records:
createRecordAsync()
– Used to create a single record.createRecordsAsync()
– Used to create multiple records at once.
In this example, we use createRecordsAsync()
to insert an array of records. As shown, the first two records include names, but the third record is empty. This will result in an empty row in the table where all the cells remain unfilled.
Looping Through and Creating Multiple Records
To insert multiple records efficiently, you can use a loop. Here’s an example of how you can loop through and insert records in batches of 50, which is considered good practice to prevent errors or performance issues when working with large datasets:
Code Snippet
while (update_array.length > 0) {
await table.createRecordsAsync(update_array.slice(0, 50));
update_array = update_array.slice(50);
}
In this code, we take the first 50 records from the update_array
and submit them for creation using createRecordsAsync()
. We then remove these records from the array and repeat the process. This method ensures that you are processing records in manageable chunks, improving performance and reliability.
Key Points to Remember
createRecordAsync()
is used for inserting a single record, whilecreateRecordsAsync()
is for inserting multiple records at once.The array of records must follow a specific format, as demonstrated.
If you pass an empty object into the
fields
, it will create a record with empty values.
Other Blogs
Other Blogs
Check our other project Blogs with useful insight and information for your businesses
Other Blogs
Other Blogs
Check our other project Blogs with useful insight and information for your businesses
Other Blogs
Other Blogs
Check our other project Blogs with useful insight and information for your businesses