If you have been developing with Visual Builder, you have no doubt interacted with the Action Chain mechanism of handling events. Action Chains allow you to connect "Actions" to one another in a "Chain" to complete a task. Common Actions include navigating around the app, or calling custom functions as described in this previous blog and a naive (or simple) approach to Action Chains would be to create a sequential path from the first to the last Action. But did you know you can also insert Logic Actions in to your chain to branch or iterate?
If you scroll a bit further down the available Actions palette you will run into a section called Logic. Logic allows you to add several standard programming concepts visually into your application. The Logic Actions available are, ForEach, If, Return, Run In Parallel, and Switch.
If you know al least one programming language, these logic concepts should be self-explainable but I'll provide a quick definition and example for each.
ForEach
ForEach is a looping construct. It takes an array parameter and will iterated over each element in the array from [0..n], the first item to the last. You use ForEach anytime you want to perform an action on each item in the array or if you are looking for a state or condition in the array to identify, separate, or sort the list, for example Find all Addresses in California, or Find all customers in California who have agreed to receive communications. Use $current.data to access the currently indexed data elements and $current.index to obtain your current index value.
If
If is a simple conditional check. Typical evaluation expressions like equals, more than, contains can be used. Any Javascript expression that evaluates to true or false can be used and depending on that outcome will branch accordingly.
Return
Return is used as a way to send back a custom value from the end of an Action Chain. Return is a terminal action so use when you want to complete a branch of an Action Chain. Although you can terminate a branch with other actions, Return is useful to specify a custom value on its completion.
