FoMS Experts

News / Free Mule Apps

FOMS APP 002

If mule, then choice!

If mule, then choice!
Share
FacebookGoogle plusLinkedInStumbleuponTwitter

PHOTO: FOMS

C. Cavalcante / E. Schatz12.6.2020

Welcome to the second FoMS Mule app! Since you are here again, we assume you had fun with the previous application. In our second Mule app. we will learn how to use if conditions and already at the beginning, we are at the best part - why to name a simple if condition as if, when we can call it a choice. Wow! We hope someone had fun. Anyhow, our choice the magnificent or a simple if condition, as most of us still call it, is here for us to learn it, so let's begin! You can download the application here.

The prerequisites

In order to understand this app, you need to have the knowledge from the following articles:
- The Prerequsits for Free Friends of MuleSoft Apps
- FoMS App 001: Hello, variables!

Used elements

In this example app, we will be using the following components:
- HTTP Listener
- Set variable
- Choice

Step 01

As shown in FoMS App 001, start Anypoint Studio, create a new Mule project and name it foms002-if-mule-then-choice!

Step 02

Add HTTP Listener and configure it with the following data:

Connector configuration > Base path: foms002
Path: if-mule-then-choice

Your FoMS application is now accessible behind the URL

http://localhost:8081/foms002/if-mule-then-choice

PHOTO: HTTP LISTENER CONFIGURATION

Step 03

Add Set Variable component in the flow, right after the HTTP Listener and configure it with the following data:

Name: city
Value: payload

Don't forget to switch the expression mode on, by clicking the fx button.

PHOTO: SET VARIABLE CONFIGURATION

Step 04

Use search in Mule Palette to find a component names as Choice and add it to the flow as the next component right after the Set Variable component. After successfully adding this new component to your flow, in front of you, you should have a situation from the picture below. The Choice component is an if condition with two predefined blocks: when and default, where the when block is going to be executed when the if condition is going to be true, and the default block plays a role of an else in an if condition.

And again and again, we ask ourselves what would be an advantage of choice-when-default before if-then-else, that everyone uses, and we still haven't find it. If you know what is the advantage of this unnecessary complication regarding naming in this part of Mule apps, please let us know.

Select when block and in the Expression field enter the following condition:

vars.city == "Berlin"

In the upper condition, vars.city is a flow variable, that we created in the Step 03 of this app, == is a comparison operator and "Berlin" is a simple string.

PHOTO: EMPTY CHOICE COMPONENT IN A FLOW

Step 05

Add Set Variable to the when block and configure it with the following data:

Name: result
Value: You live in the capital of Germany.

PHOTO: SET VARIABLE COMPONENT IN THE "TRUE" PART OF THE IF CONDITION

Step 06

Add Set Variable to the default block and configure it with the following data:

Name: result
Value: You do not live in the capital of Germany.

PHOTO: THE COMPLETED FLOW OF THE APP

Step 07

Now go back to the HTTP Listener configuration and put a variable vars.result into to response field.

Step 08

Your app is now ready to be tested with Postman. Start the app and use Postman to put Berlin in the body of the HTTP request to execute the when block of the choice component and use something other then Berlin, to execute the default block!

Have fun with the app, memorize key details and stay tuned for more apps soon!

RELATED ARTICLES

OTHER ARTICLES

Register now