Now if you are coming from the previous article, you already know about Scripting in Airtable. If not then I would like to recommend you read the first article and then continue. With that being said, Let's learn how you can Load records in airtable Scripting.
First, install the Scripting app which I already explained in the previous article. Then Copy and paste the below code into the Scripting app. Let's assume that we have a table called 'Teachers' that contains 'ID, Name, Email & Class' fields.
let table = base.getTable('Teachers');
let query = await table.selectRecordsAsync();
output.table(query.records);
In the first line of code, we are declaring a variable that is 'table' and we are selecting the 'Teachers' table from my base. Then in the next line of code, we are executing the code and in the 'query' variable we are storing the results. Finally, we are printing results in the last line of code. There is another way you can print records like 'console.log(query.records)' but it's a bit complicated if you are new to programming. In 'output.table' format, it prints in a tabular format as you can see in the below Image.
Load Records in Airtable Scripting 1.jpg
We have 2 functions to fetch data
In the first function, you can pass the id as a parameter to fetch a single record. An interesting one is the second function where you can pass parameters to sort or you can choose to get only specific fields. Let me explain to you with an example.
let query = await table.selectRecordsAsync({fields:['Name','Email'],sorts:[{field:'Class',direction:'desc'},]});
As you can see in the above example, you can pass which fields you want to select and on which field you want to sort with direction. If you want ascending order then you can omit the direction part. However, you can omit everything in parameters and still you will get records but it's for those who want specific results only.
So this is how you can get records from airtable base in your scripting app. Stay connected for more learning articles in this series.
Feel free to contact me for Airtable consulting services by CLICKING HERE.