How-to: Use Break, Continue, Exit in a Workflow
Go to the Workflow Editor page by clicking on the "+ Create" button to name and create a new workflow, or by clicking on an existing workflow from the list.
- Enter a name for the workflow, for example "Print different names"
You will be automatically redirected to the Workflow Editor page, from where you create and run your workflows:
- Click on the Advanced button to expand the Start block of the workflow.
- Click on Add Variable, you will see two fields, where you can enter a Name and Default, tick the Output checkbox if you want to create a variable that will be returned to you from the system when you Run the workflow, or choose Input if you want the variable to insert data into the system:
- Click on the icon if you want to Delete the variable.
For the workflow we will introduce one default variable "myName" that will be used (input ticked) and another variable that will collect names and return them on the screen (output ticked):
First, let's add a For Each loop:
- Enter the common section:
- Scroll down until you see For Each control button:
- Drag the For Each (control block) from the list and Drop it under the Start block where a green field appears:
- For Each and End blocks will appear. In the space between them you can drag other control blocks to build your mini-workflow that will be repeat multiple times.
The block has its own name in the upper right corner - "A2". Click to rename it if you want.
First, let's enter data in the empty field, which will be used during the loop action:
The type of the data is "expression".
Start typing in the provided field and the system will perform a dynamic search, which will show you suggestions related to what you are typing.
- Choose the name of the data element from the suggestion drop down-menu:
- Enter for example random three user names in the square brackets plus the default name "myName":
Now that we have a list of names that we are going to use in the loop, we need to determine some actions on each iteration of the loop.
- Let's drag an If control block from the common section (same section):
- Place the If block inside the scope of the For Each loop (below For Each and above End For):
On each iteration the system will check the condition's state – if it is true, the actions in the if block will be executed. Else (if condition is false), you can add more action blocks bellow Else section or leave it empty and no actions will be taken into account. Type condition for execution of the If block:
Note that the structure of If control block is situated more to the right compared to the For Each block. This means that the If structure takes action inside the For loop only (you can consider it a tree structure with main "parent" node and subtrees referred to as "children").
- Drag Break control block from the common section and drop it below the If. (make sure that you know the function of break control block):
This way, on the second iteration "Jeniffer" will be current variable and if block's condition will be true, which will lead to Break action. Break will simply end the whole For Each loop:
At this point, if you execute the workflow, the system will perform the operation, but you will not notice any change done by the workflow, because you have to add an ArrayPush action block to collect the names that the loop visits in the "result" array.
To do so, drag ArrayPush action block from the common section and drop it outside the if structure and inside the loop (below End If):
You will see a block with two fields:
- In the "Array" field you should type the name of the array in which you would like to put items.
- In the "Item" field you should type the name of the item, that you want to put in the array.
In our case we should type the following:
Now, if you run the workflow, at the footer you will receive a notification of the workflow's completion. You can see that only the first name was collected in the result, because break stops the loop on the second iteration, thus leaving the array with the first name only:
- Continue control block
It is best to use Continue if you aim to avoid further actions of the loop and move directly to the next iteration.
Let's put a continue control block instead of the break block:
- Delete the break block with the three dots at the corner.
- Drag and drop the Continue control block from the common section onto the place where break was situated:
This way the loop will check If 's status and if the condition is true it will execute the Continue control block:
Continue will simply move to the next iteration of the loop, so in this case it would skip the actions of the loop below, for example the ArrayPush block. This way the ArrayPush block will not be visited so the name "Jeniffer" wouldn't be collected in the "result":
- Exit control block
It is best to use Exit if you aim to stop the entire workflow, so no further actions of the flow will be taken.
Let's put an Exit control block instead of the Continue block:
- Delete the continue block with the three dots at the corner.
- Drag and drop the Exit control block from the common section onto the place where continue was situated:
When you place the Exit block below that if, the action of Exit will be executed on the second iteration:
Exit will simply end the whole workflow. You might have already guessed that nothing more than the first name would end up collected in the "result" array:
The difference is that, if we try to complete any other actions outside the For Each loop, none of the actions below Exit will be taken into account.
Let's experiment with a random massage outside the loop. We need to add another ArrayPush block below the End For line:
In the "Array" and "Item" fields we want to add the following data:
Now if we run the workflow it would indeed exit at the second iteration of the loop and ignore everything below, including this massage:
You might consider that break and exit are doing the same action in this case. In order to see the difference, we are going to just change exit with break keeping the same structure of the program like this:
The result would not be just the name "Michael", because break will only exit the loop that it belongs to. The second ArrayPush block is situated outside the For Each loop, so the text will be collected in the "result" array as well: