Kuovonne's Guide to Scripting in Airtable
Examples of designing coding solutions

Tossed Salad Junction Records

Your donations tell Kuovonne that you value her content and want her to continue publishing.


Inspiration post:

Please forgive the formatting for this post I am typing on my tiny phone while sitting in my car. I will try to flesh this out later. For now, you must read the inspiration post to know what I am talking about.

The original author proposes a method of looping through the records to determine what junction records to create. However, that would require a lot of nested looping, and a lot of iterations that will not result in desired junction record.
loop over the fruits
loop over the veggies
loop over the colors for each veggie
An alternative is to build a hash so that you do not need so many nested loops.
My hash would look something like this. ```
{ "red": { . "fruits": [Apple, tomato], "veggies": [onion], }, "green": { . "fruits": [avacado], "veggies": [onion, spinach], }, } ```

Since each fruit has only one color, I would probably loop through the fruits first. If the hash doesn't have a key for the color yet, create the key and for that color and the corresponding object with the two keys of arrays, including the fruit for the array. If the hash had the color key already, just add the fruit to the existing array.
Then loop through the veggies. For each veggie, loop over its colors. If the hash doesn’t have a key for that color, skip it. There is no corresponding fruit. If there is a key for that color, add the veggie to the veggie array for that color.
After the hash is built, loop through each color key in the hash. For each fruit, add a junction record for each veggie. Yes, there are nested loops here, but the difference is that each iteration of each nested loop is for an actual junction record to create. There are no wasted loops for checking combos that won't work.
Other notes
Instead of the fruit/veggie names, put the record IDS in the fruit/veggie arrays of the hash
Don’t actually create the junction records when looping over the hash. Push the records to create into an array, then create the junction records in batches.
When you query the fruits and veggies tables for their records, give the record IDs from the linked record field, and only get the values for the color field.
When you read the color field values for the fruits, you can use .getCellValueAsString() since there is only one color. However, when you get colors for veggies, use the regular .getCellValue() so you have an array of colors. Then map the select objects to their names.

For reference, this is how little I can see as I am typing this. Plus, the options for adding headings, call outs, and code blocks doesn't seem to be working.

The content in this guide is free, but creating it takes time and money. If you like this content, .

Want to print your doc?
This is not the way.
Try clicking the ⋯ next to your doc name or using a keyboard shortcut (
) instead.