Member-only story
Are your formulas working too hard?
How I replaced a slow, multi-step loop with a single, elegant command
Even after years of building in , I still have moments where I over-engineer a solution. My recent task was a simple SKU lookup between two tables. My logic for what needed to happen was correct, but my initial formula for how to do it was suboptimal. I built a system to count my rows and loop through them by their number, completely forgetting that Coda’s ForEach()
lets you iterate directly over the list itself. It was a perfect reminder that the simplest solution is usually the right one.
Before we get to the clean solution, I think it’s valuable to examine where I started. Here is the initial, working but suboptimal, setup I built to link the [My Products]
table to [All Products]
.
[My Products].Filter(Sku.IsNotBlank()).WithName(record,
Sequence(1,record.Count()).ForEach(CurrentValue.WithName(nbr,
[All Products].Filter(SKU.Nth(nbr).Contains(record.Sku)).WithName(existingSKU,
If(existingSKU.IsNotBlank(),
existingSKU.Nth(nbr).ModifyRows(
[All Products].name,record.Nth(nbr).Name,
[All Products].desciption,record.Nth(nbr).Description),
[All Products].AddRow(
[All Products].name,record.Nth(nbr).Name,
[All…