---
schema_version: "1"
id: 7be808917acdc2301955c856652c0b6586b38b67331f42aecf2f16918d6ee69b
title: Choice (<choice/>)
collection: mulesoft
version: current
locale: en-us
portal_url: https://docs.salesforce.com/mulesoft/current/en-us/anypoint-code-builder/acb-component-choice.md
canonical_url: https://docs.mulesoft.com/anypoint-code-builder/acb-component-choice
source_site: docs.mulesoft.com
content_type: documentation
content_hash: 5b2e3a6a8421c7739611445795f3799342855a22aa1307b04eabee847a94e4fe
description: MuleSoft Documentation Site
latest: true
---
---
product: Anypoint Code Builder
version: master
---

# Choice (<choice/>)

> For the full documentation index, see: https://docs.mulesoft.com/llms.txt

Route messages dynamically through a flow according to a set of DataWeave expressions that evaluate
message content.

Each expression is associated with a different routing option. The effect is to add conditional
processing to a flow, similar to an `if`/`then`/`else` code block in most programming languages.

Only one of the routes in the Choice router executes, meaning that the first expression that
evaluates to `true` triggers that route’s execution and the others are not checked. If none of the
expressions are `true`, then the default route executes.

To configure the Choice router, specify one or more conditions to evaluate, and the message
processors that execute when any condition evaluates to `true`. Additionally, configure a default
route that executes when none of the defined conditions are `true`.

## Component XML

This component supports the following XML structure:

```xml
<choice doc:name="Choice" doc:id="qwolaa" tracking:enable-default-events="true" >
  <when expression=${expressionToEvaluate}> (1)
    <!-- Message processors --> (2)
  </when>
  <otherwise>
    <!-- Message processors --> (3)
  </otherwise>
</choice>
```

<table><tbody><tr><td><i data-value="1"></i><b>1</b></td><td>The <code>expression</code> attribute has the condition to evaluate.</td></tr><tr><td><i data-value="2"></i><b>2</b></td><td>Add any number of message processors to execute if <code>expression</code> evaluates to <code>true</code>.</td></tr><tr><td><i data-value="3"></i><b>3</b></td><td>Add any number of message processors to execute if none of the conditions evaluates to <code>true</code>.</td></tr></tbody></table>

Choice (`<choice/>`) attributes are configurable through the UI and XML:

<table><colgroup><col> <col> <col></colgroup><thead><tr><th>Attribute Name</th><th>Attribute XML</th><th>Description</th></tr></thead><tbody><tr><td><p><strong>Choice</strong> (default)</p></td><td><p><code>doc:name</code></p></td><td><div><div><p>Editable name for the component to display in the canvas.</p></div></div></td></tr><tr><td><p>N/A</p></td><td><p><code>doc:id</code></p></td><td><div><div><p>Automatically generated identifier for the component.</p></div></div></td></tr></tbody></table>

Choice (`<choice/>`) has the following child elements:

<table><colgroup><col> <col></colgroup><thead><tr><th>Child Element</th><th>Description</th></tr></thead><tbody><tr><td><p><code>&lt;when/&gt;</code></p></td><td><div><div><p>Child element for specifying a condition to meet before running embedded components to process the Mule event. One or more <code>&lt;when/&gt;</code> elements are allowed.</p></div></div></td></tr><tr><td><p><code>&lt;otherwise/&gt;</code></p></td><td><div><div><p>Child element that runs if no conditions set within any <code>&lt;when/&gt;</code> element are met.</p></div></div></td></tr></tbody></table>

The child element `<when/>` has the following attribute:

<table><colgroup><col> <col> <col></colgroup><thead><tr><th>Attribute Name</th><th>Attribute XML</th><th>Description</th></tr></thead><tbody><tr><td><p><strong>Expression</strong></p></td><td><p><code>expression</code></p></td><td><div><div><p>DataWeave expression used to evaluate input. If the expression evaluates to <code>true</code>, this routing option executes.</p></div></div></td></tr></tbody></table>

## Adding Routes to the Choice Router

By default, the Choice router has one route option that executes when the configured DataWeave
expression evaluates to `true`, and a default route that executes when none of the expressions in
the existing routes are `true`. Add more routes when you need to evaluate more than one condition
and then execute different operations depending on which condition is met.

To add a new route:

1.  Add a `<when/>` element inside the `<choice/>` element.

2.  Configure the `expression` attribute to define the condition to execute this route.

## Examples

The following example configures an HTTP listener that receives a `language` query parameter and a
Choice router that evaluates the parameter and executes the correspoding route depending on the
value.

```xml
<mule xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns="http://www.mulesoft.org/schema/mule/core"
  xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
  xmlns:spring="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd
http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd">
  <http:listener-config name="HTTP_Listener_config" doc:name="HTTP Listener config" >
    <http:listener-connection host="0.0.0.0" port="8081" />
  </http:listener-config>
  <flow name="content-based-routingFlow" >
    <http:listener config-ref="HTTP_Listener_config" path="/" doc:name="Listener" />
    <set-variable variableName="language" value="#[attributes.queryParams.language]" doc:name="Set Variable" />
    <!-- START OF CHOICE BLOCK-->
    <choice doc:name="Choice" >
      <when expression="#[vars.language == 'Spanish']" >
        <set-payload value="Hola!" doc:name="Reply in Spanish" />
      </when>
      <when expression="#[vars.language == 'French']" >
        <set-payload value="Bonjour!" doc:name="Reply in French" />
      </when>
      <otherwise>
        <flow-ref name="reply-in-default-languageSub_Flow" doc:name="reply-in-default-languageSub_Flow" />
      </otherwise>
    </choice>
    <!-- END OF CHOICE BLOCK-->
    <logger level="INFO" doc:name="Log the reply" message='#["The reply $(payload) means hello in $(vars.language)" ]'/>
  </flow>
  <sub-flow name="reply-in-default-languageSub_Flow" >
    <logger level="INFO" doc:name="Logger" message="No language specified. Using English as a default. "/>
    <set-variable variableName="language" value="English" doc:name="Set Language to English" />
    <set-payload value="Hello!" doc:name="Reply in English" />
  </sub-flow>
</mule>
```

## See Also

- [Flows and Subflows](https://docs.mulesoft.com/mule-runtime/latest/about-flows)

- [Extract Data](https://docs.mulesoft.com/dataweave/2.4/dataweave-cookbook-extract-data)

- [DataWeave Scripts](https://docs.mulesoft.com/dataweave/2.4/dataweave-language-introduction)

- [Business Events](https://docs.mulesoft.com/mule-runtime/latest/business-events)
