I wanted to write one last time about writing code (well, Airtable scripting). After doing the prep work and hosting two learn Airtable scripting series, I can safely say that I feel proficient writing scripts! It's not true proficiency: I probably can't write extremely complex scripts but I can now read, understand, and write for the vast majority of use cases for scripting.
Below, I'll share my "aha" moments but before I do, I just wanted to say how fun it is to write code without any setup. No packages to install or dependencies to worry about. Nor is there a need to fake data because you're working out of your own data. You just write code. Useful code!
The editor even does most of the work for me by autocompleting and surfacing what's possible at any moment. I planed to "plant" mistakes throughout the session but every time the scripting editor caught them before I could press run!
What helped me
The first realization is that mapping concepts from a Javascript course to Airtable scripting isn't necessarily straightforward. The usual goal of Javascript courses is to teach how to interact with CSS and HTML of pages so you can create websites. Your "data" in those courses is the HTML of the page that you loop over to find classes/divs for which you want to change CSS styling (this is how interactivity is managed on the web). In theory that isn't different from looping over Airtable data. But it is challenging to map (ha, javascript pun!) the concepts from one to the other. It's why I've found the livestreams (you can find them on twitch.tv/AirtableTV) much more informative than any course I've taken (and I've taken a few)!
Input, Output, do something
I like to think of every script as 3 broad parts: input data, do something with that data and output data.
Input data
Retrieving data is relatively straightforward in Airtable scripting, simply create a table variable and then retrieve the records you need like so
Output data
I've found it helpful to first think about what I want to output before I think of what needs to happen from input to output. The output of most Airtable scripts is to update specific records (see stream #1) or to create records (see stream #2). To do that in Airtable scripts, you have to create an array that has the information needed so Airtable knows what records to create (or to update). For creating records that's an array with field information (for update it's the same but with recordIds of the records you want to update).
So your penultimate step is usually looping over an array using the map method and getCellValue to get to the array in the right format.
The tricky part here is knowing how to input information for each field type. The documentation here but can be a little confusing to a neophyte like me. What I've been doing is practicing for each field to understand how the array needs to be formatted to update/be created correctly!
Do something
The middle part is where you do what you need your script to do! I can't quite predict what your script needs to do but invariably you'll need to find records in your base that match certain conditions.
A cursory understanding of arrays and a few array methods gets you most of the way there.
How I understand arrays (and this may irk developers) is that they're a list of objects. Those objects can contain arrays or other objects. For instance, here's what retrieve a list of records returns:
[caption align="alignnone" width="980"]
Now you have two arrays: 1/ allProjects.recordIds that will return an array of recordIds in your table and 2/ allProjects.records that is an array of objects containing an id and a name. You can now loop on those two instances (allProjects.recordIds and/or allProjects.records) to retrieve information, compare it, update it etc.
Looping
Now that you have your list of records, to loop on them and "do things" you can use array methods! These are convenient shortcuts in Javascript that let you do a specific action. The ones I've found most useful and common are find, filter and map. Here's how I understand each one:
let newProjects = allProjects.records.filter (project => (condition))
The filter method will loop over each record (using the project variable) and return (insert in newProjects) all records that match a certain condition. It's useful for finding a subset of records in your table that match certain conditions!
let newProjects = allProjects.records.find (project => (condition))
Find is similar to filter in that it will loop over your records (using the project variable) but instead of returning all records that match a condition, it will only return the first instance. It's useful for checking if something is true (is there another record with the same email?).
let newProjects = allProjects.records.map (project => (function))
The mecca of methods: map will loop over your records and execute a function. I've mainly used it in to create arrays that have the data I need to create/update records.
If you understand how to retrieve data, output data and how to use those three methods, you should be pretty much set to get comfortable writing Airtable scripts!
More scripting?
We've got 2 more sessions on live Airtable scripting happening on AirtableTV. I'd love to keep writing scripts live on my own channel but I'm wondering if there's enough interest to make it a recurring session on the stream.
Do you want to learn Airtable scripting? Would you watch more live Airtable scripting? Do you have a script you're struggling to write and want me to try to write it? If you answered yes to any of these, let me know by replying to this email or reaching out on twitter. If there's interest, I'll add some sessions to the calendar!
Best
Aron