If/else statements and forms

Hi all,

Is it possible to automatically insert the results of an "If/Else" statement into a "Form" (e.g. formtext)? For example,

{if: contains(X, "Name 1")} 1A {elseif: contains(X, "Name 2")} 2A {endif}{formtext: name=result of if/else statement}. I know you can create other formulas that can insert values, such as {Text={=extractregex(site:text, "Name: (.*)")}}{formtext: name=FullName; default={=Text}}

I often use a single form to adjust a value across multiple other formulas but I also want the field to be dynamic and not fixed as sometimes I want to manually update the formtext field as well.

Hi @Josh_Whitchurch - great question.

Would something like this (or some kind of adaptation of that workflow) work for you?

The snippets used in the video are linked in the video's description.

Have a look at it and let's discuss how we can adapt it to your particular workflow.

Hi @Cedric_Debono_Blaze ,

Thanks for your response! So not quite. I've tested a few different ways to try and accomplish what I'm trying to do but I'm not sure it's currently possible.

{id=extractregex({site: text}, "Name:(.*)")}

{if: id == "Name1"}
ID 1
{elseif: id == "Name2"}
ID 2
{endif}

{formtext: name=ID; default=ID 2 or ID 1, etc.} Essentially, I am trying to import the result of the IF statement into the form.

I've also attempted to use the {import} option with an IF/Else snippet that I've created but it pulls in the whole snippet, as I'm sure is expected. Although, it does import the stand alone results if I include the {import:/snippet} within the same snippet. Does that make sense?

For example,

This works: {formmenu: name=ID; default=XYZ}{import:/snippet}

  • This will execute the IF statement and import it into the form popup window (e.g. ID 1 will appear).

Does not work: {formmenu: name=ID; default={import:/snippet}}

  • This does not work and will import the entire contents of the snippet into the form versus the results of the IF statement (e.g. {id=extractregex({site: text}, "Name:(.*)")}....will appear).

Hi @Josh_Whitchurch,

The default setting won't allow any commands, that's why your workflow isn't working.

However, you could accomplish something similar with keyed lists.

Let's start with the basic structure.

Below, you can see a keyed list consisting of 6 items, each one having a key and corresponding value.

The value for the sixth item is the variable other, which is populated by whatever I insert in the text field (which shows up when I choose "other" in the dropdown).

{list=["option 1":"result 1", "option 2":"result 2", "option 3":"result 3", "option 4":"result 4", "option 5":"result 5", "other":other]}

{formmenu: name=choice; values={=keys(list)}}{if: choice == "other"}{formtext: name=other}{endif}
{=list[choice]}

Now, let's apply it to a scenario where we're checking for one of a number of strings.

{note: preview=no}
{list=["name 1":"ID 1", "ID 2":"result 2", "name 3":"ID 3", "name 4":"ID 4", "name 5":"ID 5"]}
{id=catch(extractregex(sitecontent, "Name:\s*(.*)"), other)}
{endnote: trim=right}
{note}
This text field represents what you'd be extracting from the website:
{formparagraph: cols=50; name=sitecontent; default=Name:}

{endnote: trim=right}
{=id if id <> "" else "No name found"}: {=catch(list[id], "No corresponding ID")}

Here's a more advanced version. In this case, if the regex extracts anything that does not match one of the keys in the list, you'll get two text fields to input the name and ID manually.

{note: preview=no}
{list=["name 1":"ID 1", "name 2":"ID 2", "name 3":"ID 3", "name 4":"ID 4", "name 5":"ID 5"]}
{id=catch(extractregex(sitecontent, "Name:\s*(.*)"), other)}
{endnote: trim=right}
{note}
This text field represents what you'd be extracting from the website:
{formparagraph: cols=50; name=sitecontent; default=Name:}
{if: NOT includes(keys(list), id)}
No name matches.
Please enter name and ID manually.

Custom name: {formtext: name=customname; default=N/A} {if: customname == "" OR customname == "N/A"}{error: Please specify a name; block=yes}{endif}
Custom ID: {formtext: name=customid; default=N/A} {if: customid == "" OR customid == "N/A"}{error: Please specify an ID; block=yes}{endif}
{endif}

{endnote: trim=right}

{=concat(id&": "&list[id]) if includes(keys(list), id) else concat(customname&": "&customid)}

If you wanted, you could take it a step further (assuming the platform you're using allows it) and use {urlsend} to send the contents of the text fields to a google sheet, so you can track new name/ID combinations. And then instead of specifying the name/ID pairs inside the snippet, you could use {urlload} to load them from the google sheet.

Would this work for you?

Thanks @Cedric_Debono_Blaze !

Yes, this helped. Actually, I didn't even think about using a keyed list to solve for my problem :man_facepalming:. That was exactly what I needed. I created my If/Else snippet a while ago so I was stuck on making that work, which it did for what I used it for.

Also, my snippet was fairly long too due to the multiple values I needed to add, etc. so I didn't want to create a new long list but I just used the replace function to quickly turn my If/Else statement & values into a list.

1 Like

@Josh_Whitchurch - nice thinking!

As I always say, using Text Blaze is very much like building a muscle. The more you use it, the more your mind develops a certain way of thinking. You start looking at workflows differently and approaching challenges in new ways.

Personally, that's the thing I love the most about Text Blaze (aside from the insane amount of time it saves me).