Conditional paths in workflows

Conditional paths in workflows

Compose Enterprise is going through a design update. The illustrations on this page might be of the old design, but the information is up to date. Thank you for your patience during this update!

Norsk

What is a condition?

It is possible to have condition activities in a workflow. Adding a Condition or Multi Condition activity to a workflow enables you to send respondent users through different paths in the workflow. What separates the Condition from the Multi Condition is that a Condition enables two paths, while a Multi Condition enables as many paths as you want.

Condition activity in a workflow

In this first example we will use the Condition activity to create two paths in a workflow. Depending on the answer(s) given by the respondent user, some of the users will be sent straight to the end point, whereas others will fill out a second form before reaching the end point.

Create or open a workflow including a start point, a form activity, a condition activity, another form activity and an end point, just as the workflow below. If you need help setting up the these activities, take a look at the Build a simple workflow user guide. 

First we need to determine which answer in the first form will define the condition in the workflow. We do that by connecting the form answer to a variable which we use to script the condition. Select the Condition activity in the canvas and click the Script variables button.

In the Variables menu, add a variable by clicking on the V icon and give it a variable name.

The variable's type information needs to be defined. Type will determine whether the variable can be mapped to a single value with a validation (in our case String) or an entire form. In this example we will have a value type variable from the workflow, but scroll down and you’ll see an example script when using the form value type. Choose the Value From Workflow radio button and click Browse to find the question controlling the condition.

Expand the listed activities by clicking on the arrowheads to reveal the form questions (or workflow variables if you expand Workflow Variables). Select the question you’d like to control the condition and click OK.

An important thing to notice in this example is that we have connected the variable to a single select question (radio button/dropdown) with two options with answer values 1 and 2. Now look at the reference in the Value field: Form 1.Q3.answer.value. This means, the variable is now mapped to the answer value (not the answer text) of the question with ID Q3 in the workflow activity Form 1. This means we need to reference the answer value when we use the variable questionAboutChildren in the condition script. Click OK to save the settings.

The script’s result needs to be true or false, so our condition is a statement:

questionAboutChildren == "1"

This implies that if the respondent user chooses the option with answer value 1 the condition is true and the respondent user is sent to one path in the workflow. Any other answer than the option with answer value 1 is false and will send the respondent user to the other path in the workflow. Note: Since we validated the variable value type as a String and we must add quotes to the answer value (“1”). If we’d chosen the validation Integer, then we could’ve written the script without quotes (questionAboutChildren == 1).

Now we connect the true and false scenarios to the two connectors implying different workflow paths. We want the true condition to send the respondent user to fill out Form 2. Select the connector to Form 2 and in the Direction dropdown choose 1 - if. The logic between the two options if and else is if the condition is true then 1 - if is the continued path. Else it’s false than 0 - else is the continued path. If the script fails to execute or not returning a boolean value (true or false), then the workflow will default to false (0 - else). By selecting the direction of one connector the other automatically changes to the other direction.

Remember to save draft and you have a functional condition in your workflow!

Multi Condition activity in a workflow

If you need more than two out-going paths the Multi Condition is more practical. In this example we will use the Multi Condition activity to send respondent users through three different paths in a workflow (to three different email activities). The method of creating variables is equal for multi- and regular condition activities, so follow the guide above until the point of scripting the condition.

The variable test is a value variable type from the workflow connected to the answer value of the single select question (radio button/dropdown) with question ID Q4. The question has three options with answer values 1, 2 and 3. Click OK.

In the Condition activity, the if/else statement as it’s called in the world of coding was implied and also indicated in the Direction dropdown of the connectors (if - 1 and else - 0). However, as there are more than two outcomes or workflow paths, we need to script the if/else statement in the Script window. We divide between the different paths with enumeration starting from 0. Select the connectors to see that the three connectors have the Direction value 0, 1 and 2.

Therefore, when we make a statement in the condition script, we also need to return the direction number to indicate which path the workflow should choose if the statement is true. The code will then for the variable which can take the three different answer values 1, 2 and 3, look like this:

if (test == "1"){ return "0"; }   else if (test == "2"){ return "1"; }   else if (test == "3"){ return "2"; }

Note: Press the Open button under the Script window, to be able to see larger parts of the entered script at the same time. Here is a translated version of the code:

  • If the respondent answer value is 1 the connector with the direction 0 will be chosen.

  • If the respondent answer value is 2 the connector with the direction 1 will be chosen.

  • If the respondent answer value is 3 the connector with the direction 2 will be chosen.

By default the workflow will run through the connector with direction 0 if no answer is given to the form question.

Double-check that the direction of the connectors is correct by selecting the connectors and checking the Direction dropdown. Save the draft and you have a functional multi condition in your workflow! This multi condition example is attached in the workflow here: Workflow_multi_condition.cng. The workflow can be imported to Compose version 5.16.0 and newer.

Form type condition script

In the two previous examples, we’ve only mapped the variable to a question answer in a form. However, you do have the option to map the variable to the entire form and reference the different questions within the form in the condition script. In the Variables menu to the right, the variable orderValue has the type Form and is mapped to the form with the workflow name FlowObject_3. If you’d like to reference the question with question ID Q2, you could do it with the condition script below:

orderValue.Q2.answer.value == "1"

The variable orderValue represents the complete form. You may reference any question answer with their question ID.

You are of course not limited to creating only one variable, only referencing single select questions etc., so check out the Condition scripting user guide to see more of the available possibilities in Compose.