May 26, 2021 11:13 AM
In the process of building an Airtable AUTOMATION that will update multiple records at once on a daily schedule, I discovered that’s not currently supported. That leads me down the path of trying to run a script that will run on said daily schedule within the AUTOMATION. However, I have a very novice understanding of JavaScript and I’m struggling to apply Airtable’s specifics when it comes to writing a script.
Is there anyone willing to help me out? I tend to learn a TON every time I have the opportunity to modify a script. But I need some help getting there.
HERE’S WHAT I NEED A SOLUTION FOR:
On a daily basis, I’d like to change the {Status Field} —which is a “single select” type— of a list of records (when the status is “Option 2”) to have a status of “Option 1”).
My knowledge falls short on syntax. So I don’t know if it’s easier to comb through the entire table using a “for loop” or calling a view that does that work for me. But I’m lost when it comes to much more than identifying the variables of the table.
Anyone who can help with an example script that can achieve this (that I can modify)?
May 26, 2021 11:31 AM
You should use Airtable’s views to do filtering for you. Set up a view that filters to only have records that have the value Option 2
// Edit the names in quotes
let table = base.getTable("Table 1");
let view = table.getView("Name of view");
let selectFieldName = "Single Select field name"
let replacementValue = "Option 1"
// No edits required below this line
let query = await view.selectRecordsAsync()
let records = query.records
let updates = records.map(record => {
return {
id: record.id,
fields: {
[selectFieldName ]: {name: replacementValue}
}
}
})
while (updates.length > 0) {
await table.updateRecordsAsync(updates.slice(0, 50));
updates = updates.slice(50);
}
May 26, 2021 11:50 AM
@Kamille_Parks Thank you, thank you, thank you!!! This works perfectly!
In my quest to understand why this works, I don’t quite understand how to learn about the “while” function. Is that specific to Airtable? or is that a JavaScript thing that’s clearly outside my scope?
May 26, 2021 11:53 AM
while() {}
is regular Javascript. Its a type of loop function, similar to for() {}
Airtable scripts (and Airtable custom apps) are limited in the number of record creates/updates they can do at once. The cap is 50 records, so the last bit of the script takes all the updates needed in 50-record batches and stops when the number of updates left is zero.