Conditionally Taking an Action Within FormulaMap?

I’d often like to use a pattern like: FOR EACH [item], IF [condition] THEN [action], where no action is taken if the condition isn’t true. The hidden formula _NOOP can accomplish this, but this document is too important to risk using hidden formulas.

I found this thread from 2021 that indicates SwitchIf() can be used for this purpose because the default value is optional. That seems to be true, but as soon as the SwitchIf() that occasionally defaults to ‘no action’ is placed within a FormulaMap, it doesn’t work- the first time the condition isn’t true, the formula exits with an ‘Invalid Action’ error. Example here.

I’ve been handling this with a clunky approximation of _NOOP; whenever I want no action to be taken I have the formula delete rows from a dummy table that exists only for this purpose. It works, but it seems like there must be a better way.

2 Likes

hi @Joel_Tennyson1 ,

Your observation is in line with my experience, even in the SwitchIf() we need the _Noop() in this scenario when the evaluation returns false. Leaving the fall back option empty or putting "" or List("") does not work either.

May you feel uncertain, you can create a value in the fall back option you filter on as condition for a delete action.

RunActions(
thisRow.FormulaMap(
  SwitchIf(
    currentvalue.Value = "Cat",
    Results.AddRow(Results.Result, "You added a Cat."),
    Results.AddRow(Results.Result, "delete me.")
  )),
  Results.Filter(Result.Contains("delete me.")).DeleteRows()
)

I hope it helps,
Cheers, Christiaan

_Noop() is okay and won’t break — it is heavily used internally. Tell you this, your whole button becomes a single _Noop() formula when it is disabled okay not really anymore :slight_smile: previously it was a pendingevaluation object as well, now it’s a sentinel value.

Wondering if anything changed recently because contrary to what Christiaan said, "" used to work fine in place of _Noop(), i.e. you could totally If(expr, Action(...), "")

P.S. Just checked, yeah, no problem whatsoever here
image

2 Likes

morning Paul,

I tested this variation based not the test doc of @Joel_Tennyson1 (again) and the code below does not work.

thisRow.FormulaMap(
  SwitchIf(
    currentvalue.Value = "Cat",
    Results.AddRow(Results.Result, "You added a Cat."),
    ""))

Cheers, Christiaan

Does anybody know why _Noop() is not officially supported or mentioned in the documentation when it’s heavily used internally?

@Christiaan_Huizer, what do you mean it doesn’t work? What error do you get?

Can you please share the doc with me at paul@codatricks.com? I’m curious to take a look.

Maybe FormulaMap/ForEach introduces some bug because I remember there were some. I’m not using "" myself so I might not be in the know.

@Yannik, I think two reasons.

Reason #1 is that they could be looking for a less technical or generally more elegant way to set up a noop action — an average person wouldn’t know what noop means, and even not every developer would. I suspect that the support for "", which was added sometime in 2022, could actually be that elegant solution that they were seeking.

And reason #2 is… I dare you to find a single video from Coda themselves where they are teaching how to set up buttons with complex actions. Because all I’ve seen so far were Maria’s videos where to make multiple actions, she’d advise to make a button that clicks other buttons. And to make those buttons conditional, Coda would advise to just use the “disable if” formula. That’s why I made a special focus on these in my video about buttons. I suspect that Coda prefers to treat that flexible custom actions feature as something too pro, and since there is already a workaround for pros with the _noop(), they wouldn’t prioritize making it more accessible to beginners just yet.

1 Like

okay, doc shared with you @Paul_Danyliuk
cheers, christiaan

Thanks for your reply. Do you know why Coda generally seems to be reserved about the outside world when it comes to the more advanced themes?

Huh, yeah, seems like when a button produces a list of actions but there are blank items (i.e. "") that’s where it would fail now. I think it’s introduced recently, like a few months ago, because I’ve seen a similar behavior change with some other code that I had to go and fix, I don’t remember now exactly what that was. In a nutshell, Coda got stricter with what it expects as a result of a button action formula: it has to strictly be a single action or a list of actions (or a one big blank) but not a list of lists anymore, and not a list with blanks in there (`_Noop() is not a blank, it’s still an action, which simply does nothing)

Well, since in your formula you’re building a list of actions, you can just filter out the blanks — this works:

thisTable.FormulaMap(
  If(
    CurrentValue.Value = "Cat",
    Results.AddRow(Results.Result, "You added a Cat."),
    ""
  )
).Filter(CurrentValue.IsNotBlank())   // <- this will clean up the blanks

@Yannik I’m not a codan and cannot really answer that. From what I’ve heard, all the analytics and customer discovery that Coda conducts show that even stuff like this is above the average of what average users are doing with Coda. So we might be a minority at the moment. Things will probably change as the adoption grows and more power users get onto it.

1 Like

Thanks everyone! This has been immensely helpful. Filtering out blank results after the FormulaMap seems a lot cleaner than modifying a dummy table.

2 Likes

Experimenting a little bit taught me that there is a shorter (perhaps more elegant?) way to write this formula:

thisTable.FormulaMap(
  If(
    CurrentValue.Value = "Cat",
    Results.AddRow(Results.Result, "You added a Cat."),
    List()  // instead of ""
  )
)

Presumably this works because the button wraps your formula with RunActions(ListCombine(...)), which ignores empty lists.

4 Likes

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.