Free MuleSoft CONNECT Keynote & Expo Pass Available!

Register now+
Nav

Choice Router

The Choice flow control component dynamically routes execution based on the contents of its input. It adds conditional processing to a flow, similar to an if/then/else code block in most programming languages.

A Choice flow control component uses expressions written in DataWeave language to evaluate the content of the input. It then routes to the first option that matches the expression (evaluates to true).

  • A When (when) block is executed when the input matches its condition.

  • The Otherwise (otherwise) block is executed by default if none of the conditions match the When block.

Choice Router Schematic

Here, the top routing conditions are set by when blocks, while the default is set by an otherwise block.

Example

In this simple example, the Choice router is returns a message in one of three languages depending on the value of a language variable in the Mule event.

Choice Router Example

Note that you can provide a custom name to each condition, which only affects what you see in the editor, not the behavior of the app.

The configuration for this router shows the DataWeave used for the conditional processing:

  • #[vars.language == 'french']

  • #[vars.language == 'spanish']

  • Default to reply in English.

Choice Router Configuration

The components within the router set the value of the response. So, if the value of the language variable is spanish, the output might be Hola! instead of the French or English greetings.

In this topic: