Choice Router In MuleSoft
Pre-requisites
Anypoint Studio:
- Anypoint Studio should be set up.
Database:
- We need to have a database.
- For this instance, we’ll be using the MySQL database.
- Create a database with some name and inside the database create a table with some fields as per the requirement.
- For this instance, we’ll be having the table name as xlsx_db and the fields are EmpId, EmpName, Contact, Designation, and Age, where EmpId is the primary key.

Choice Router In MuleSoft
- Routers enable developers to set conditions that determine the processing and destination of messages inside the integration flow.
- A choice router is a part of the core module, which falls under the category of flow control.
- It is the component that is used for achieving conditional routing in MuleSoft.
- The choice router will route the control to the first matching true condition and there is no restriction on when blocks, we can have multiple when blocks.
- There is only the expression in the when block that needs to be configured:
- Expression: Expression is the condition in which if it gets satisfied then the when block will execute or if this expression does not get satisfied then in that case default block will get executed.

- In this blog, we’ll see how to achieve conditional routing in MuleSoft. The requirement is to upsert the record into DB using the choice component.
*Note: First we’ll check whether that particular record is present in the DB, if it is there then we’ll update the record, else we’ll insert the record.
- For this requirement, we’ll be using MySQL DB.
Steps:
- To begin, a new Mule project needs to be created. Then, a HTTP listener component dragged and dropped into the project from the HTTP module. Configure the listener, and set the host to All interface (0.0.0.0), port number as 8081. Set the path as /upsert.

2. Following the addition of the listener component, include two loggers to mark the beginning and the end of the flow. By logging the message “Start of flow” ++ (flow.name as String), in expression mode.

3. Now we’ll add select component and configure it. We’ll add the SQL query and input parameter for the query.


4. Next, we’ll click on the advance section of select component and add a target variable to store the response of the select component.

5. Then we’ll add a Choice router and configure the expression as vars.record == []. This expression means the matching record does not exist in DB, as the DB will always return empty array if the no matching record is found.

6. Next, we’ll place an insert component inside when block to store the record into the DB and in the default block we’ll place a update component to update the record if the record already exists in the DB.


7. Now we’ll add a transform message to transform to both the blocks to send a static response which is “record has been created” in case of insert and in case of update the response will be “record has been updated”.

8. The application is ready to be executed. Let’s hit the request using Postman and verify using the DB.



