Sometimes in Bubble, we want to do one thing or the other. In some cases, it’s not possible to check for both cases in a single workflow, because the condition can change after an action…
For Example
-Imagine we have custom state that holds a list of images.
-When we click on an image, we want to add it if it’s not already in the custom state, and remove it if it is.
This cannot be achieved in a single workflow, because bubble doesn’t have an “else” system, only an “if”.
The workflow would run like this:
-Action 1 (For adding images)
- Check if custom state doesn’t contain the image
- If it doesn’t, add the image
-Action 2 (For removing images)
- Check if custom state contains the image
- If it does, remove the image
Both checks will be true, because the image won’t be in the custom state at first, so it will be added, which results in the second action to also be true, which will remove it again…
There’s 3 ways to solve this:
-Using an “else” statement
-Two different “on-click” workflows that checks a different condition
-Evaluating the condition before any other action is taken and saving it as a static true or false (yes/no) value.
Since building 2 workflows for a simple action adds up over-time and since we can’t use “else” in Bubble (yet), we use the third option.
The Third Option
It’s very simple really! All we do is create a custom event that evaluates the condition and returns yes or no. We then use the value of the custom event to determine which actions to take, because the value won’t change.
1) So we tried this first, and realized it wouldn’t work:
2) We then tried separating it into 2 different workflows, which works, but isn’t ideal in the long run:
3) But we then realized the best solution is a custom event:
-Give the custom event a new parameter with data type yes/no.
-Also give it a return value of yes/no
-The return value is going to be the evaluated “condition” parameter that you are sending the custom event:
-Now just use the custom event!
Hope this helps!