Create Your First Mule Application with Flow Designer

You are the connectivity lead for Salesforce Sales Cloud at Northern Trail Outfitters (NTO). NTO makes stylish and comfortable apparel, and gear for outdoor recreation and fitness.

NTO uses Sales Cloud to track the opportunities in the pipeline for bulk swag orders. After they close opportunities, sales reps need to go into the order management system and place the orders. Also, their customers often call NTO to check on orders.

NTO wants to use MuleSoft to create a process that places orders automatically when opportunities are closed, and that notifies customers via SMS about their order status and ETA.

Your task is to put this process in place within a tight deadline. You realize that it is critical for four systems that NTO uses to be integrated with each other for this process to work

System Purpose

Salesforce Sales Cloud

Manages sales opportunities

MySQL database

Stores product information and SKUs

NTO Order system

Internal system that can place orders and return tracking information + order ETA. Callable through the NTO Order API.


Sending notifications to customers

Your approach

To automate this process, you have to overcome a few integration challenges. You come up with this list of what needs to happen in order to automate this order-to-cash cycle.

  1. Salesforce starts the workflow automation process when a sale is made. This process is triggered in Salesforce when an opportunity is changed to "Closed Won".

  2. The process retrieves the appropriate product information from the products database.

  3. After the process has retrieved the information, it places an order with the NTO Orders API.

  4. The Orders API returns the order number, tracking information, and ETA for delivery.

  5. The information returned by the API is translated into human readable text and sent as an SMS to the customer.

Visually, the process should look like this:

salesforce to twilio flowchart

Fortunately, you know just the tool to help you carry out this plan: Flow Designer!

Before You Begin

Capture opportunities that are won

You need to ensure that your application is triggered as soon as the status of an Opportunity in Salesforce is changed to ‘Closed Won’. To do this, use an SOQL query in Salesforce which fields to send and our criteria for triggering the application. After you build the query, you can set up a Push Topic that can serve as the trigger for your MuleSoft application.

  1. Navigate to the SOQL query builder. (You have to log in with your Salesforce credentials.)

  2. Select Opportunity as the Object.

  3. Select Amount, CloseDate, Id, and Name from the Fields menu.

  4. In the Filter results by fields, add StageName = ‘Closed Won’.

  5. Test the query by using the Query button. You should get a list of all closed Opportunities. Your query should look like this : SELECT Amount,CloseDate,Id,Name FROM Opportunity WHERE StageName = 'Closed Won'

    soql query screen 1
  6. Copy the query to your clipboard.

  7. Navigate to the Streaming Push topics page from the query menu at the top of the workbench.

  8. Create a new Push Topic by selecting the option Create New.

  9. Name the Push Topic ClosedOpps and paste in the query that you built.

  10. Save the Push Topic.

    streaming push topics

Now, every time the query criteria are met, Salesforce will push an event into the ClosedOpps Push Topic.

Create a project in Flow Designer

When you are working in Flow Designer, you create your Mule applications in projects. A project can contain only one Mule application.

A Mule application consists of one or more flows. A flow, as MuleSoft defines the term, is a simple, flexible mechanism that enables orchestration of services using the sophisticated message flow capabilities of Mule runtime engine. Also referred to as service orchestration. In Flow Designer specifically, a flow is a sequence of cards that represent core components (such as loops, conditionals, transformations, and more) or connectors to systems and APIs (such as Salesforce, SAP, Google Docs, and more). Cards can represent a couple of other things, too; however, for this tutorial, you need know only about those two.

Data moves from the beginning of a flow to the end of the flow. Along the way, the cards can update systems, query systems, send messages, delete database rows, or do any of the things that core components and connectors can do.

There is a good number of core components and there are many connectors. For this tutorial, you’ll use these:

  • Salesforce connector

  • Set Variable component

  • Choice router component

  • Database connector

  • Set Payload component

  • Transform component

  • Twilio connector

For this project, you’ll create only a single flow.

Follow these steps to create the project.

  1. Log into Anypoint Platform.

  2. Open Design Center and create a new Mule application.

    1. From the Projects page, click Create new.

    2. Select New Mule App.

    3. In the New Mule App dialog, name your project Order-to-cash automation.

    4. Click Create App.

  3. In the Let’s Get Started wizard, click the link Go straight to canvas, which is in the bottom-left corner.

    Result: Flow Designer appears with an empty trigger card displayed in the middle of the canvas.

    canvas empty trigger

Configure the trigger for the flow

A trigger is an event that starts a flow in a Mule application. In your project, the trigger is the change in status of an opportunity in Salesforce Sales Cloud to Closed Won. You can configure the trigger card to represent a Salesforce connector that is subscribed to the topic ClosedOpps, which you created above in "Capture opportunities that are won".

  1. Click the Trigger card.

    Result: A list of the available connectors is displayed in the card.

  2. In the Search field, type “Salesforce”.

    Result: The Salesforce connector appears in the list.

  3. Select the Salesforce connector.

    Result: A list of operations that you can use to trigger Mule applications with the Salesforce connector is displayed.

  4. Select the operation Subscribe Topic.

    Result: The card is opened. In its top-left corner, it is identified as a Salesforce connector. The name of the operation — Subscribe Topic — is also displayed. At the top of the card is the message “You need to set up the Salesforce configuration. Click here to set it up.”

  5. Specify how you want Flow Designer to authenticate to Salesforce.

    1. Click the linked text that is in the message.

    2. In the Connection Type field, select Basic Username Password. If reusable connections are available, select one.

    3. Specify your username, password, and security token.

    4. Click the Test button to check whether Flow Designer can connect with the credentials that you specified. If the test fails, double-check your credentials and token. Save the configuration after the test succeeds.

  6. Add your Push Topic to the connector by typing ClosedOpps in the Topic field. Then, close the card either by clicking the X in the top-right corner or by clicking an empty space on the canvas.

Configure the card for Twilio

Because you want to notify the customer with an SMS, you can use the Twilio connector and its Send Message operation.

  1. Click the plus sign that is to the right of the Salesforce card.

  2. Select the Twilio connector and the operation Send Message.

    Result: The Twilio card is opened. At the top of the card is the message “You need to set up the Twilio Connector configuration. Click here to set it up.”

  3. Specify how you want Flow Designer to authenticate to Twilio.

    1. Click the linked text that is in the message.

    2. In the Username field, specify your account SID for Twilio. In the Password field, specify your authentication token for Twilio.

      Do not specify your username or password in these fields. If you do, the Twilio card returns the message HTTP POST on resource 'https://api.twilio.com:443/<Date>/Accounts/<account-SID>/Messages.json' failed: Timeout exceeded. when you trigger the flow in your app.

    3. Click the Test button to check whether Flow Designer can connect with the credentials that you specified. If the test fails, double-check your credentials and token. Save the configuration after the test succeeds.

  4. In the Account SID field, specify your account SID again. Then, close the card.

Create data types

The NTO systems all use different formats for data. Because the flow that you are creating needs to pass data through those systems, Flow Designer requires you to give examples of those data types. Because you’ll reuse a few of the data types, you can create them at the project level, rather than at the points in the flow where you need them.

Create the type for the data sent by Salesforce

  1. In the Project sidebar, click the plus icon that is next to the heading of the Data Types section.

    Result: The New Type dialog is opened.

  2. Create the data type that represents a Salesforce opportunity and that the Push Topic ClosedOpps sends to trigger the flow.

    1. Name the data type ‘Salesforce Opportunity’.

    2. Paste this JSON into the Add your JSON example field.

       "Amount": 15000,
       "Id": "0061U0000079dWXQAY",
       "CloseDate": "2019-01-25T00:00:00.000Z",
       "Name": "Dickenson Mobile Generators"

      Result: The fields from the example are parsed automatically in the right sidebar of the dialog.

  3. Click Save.

Create the type for the data sent to and returned from the Orders API

Following the same steps, create the following two data types:

Name: Order Information

The process sends data to the Orders API in this format. Here is an example to use in the New Type dialog:

       "OppId": "fsjf3439d993",
       "OrderAmount": 500000,
       "orderdate": "2018-11-13T11:15:45",
       "productInfo": {
           "productID": "CK111274",
           "category": "Small",
           "SKU": "CKS0",
           "productName": "Old School Classic Kicks",
           "inventory": 9999

Name: Order Tracking

The Orders API returns this information after an order is placed. Here is example JSON:

 "OrderID": 50122,
 "TrackingNo": "Z34342211",
 "ETA": "2018-11-13T11:15:45"

Create the type for the data stored in the products database

Name this data type Product Information and use this example JSON:

   "productID": "CK989603",
   "category": "Medium",
   "SKU": "CKS50",
   "productName": "Air Fly High Quality Sneakers",
   "inventory": 22333

Save the opportunity data into a variable

Because you’ll be using data from the opportunity later in the flow, and cards that follow the trigger will be creating their own outputs, you need to store all of the opportunity data into a variable.

  1. Mouse over the arrow between the Salesforce card and the Transform card to change the arrow to a plus sign. Then, click the plus sign to add a new component.

  2. Search for and select the ‘Set Variable’ component

  3. Name the variable OppDetails.

  4. Click Custom Expression in the list of items on the right side of the card and drag it into the Value field.

  5. Click the arrow on the right side of the expression box and click EXPAND.

  6. In the Custom Expression dialog, type payload in the Expression field.

    The expression payload indicates the entire input that arrives at the Set Variable card during one activation of the Mule app. As you can see in the right-hand pane of the card, the payload consists in this case of four data elements. It’s possible to specify use one or more of those in the Expression box, rather than the expression payload. You could click and drag those elements into the box. However, for this tutorial, we’re using the full payload.

    fd tutorial set var right pane
    Figure 1. The four data elements that make up the payload that arrives at the Set Variable card.
  7. Click OK and then close the card.

    Your flow should look like this:

    flow with set variable card

Add SKUs that are based on the size of the deal

NTO has different SKUs (stock keeping units) for their enterprise customers, small business customers, and retail customers. You need to add the appropriate product information from the product database to the opportunity based on the deal size. The table below explains how products are segmented.

Deal Size (Opportunity Amount) Product Category

More than $100,000


More than $50,000

Small Business

$50,000 or less


You can use the Choice router component together with the Database component to achieve this branching, if-else logic. For example, with the Choice router, you can specify that, if the deal size is greater than $100,000, the process should search in the database among the SKUs that are in the Enterprise product category.

The Choice router component is part of Mule Runtime. There are several other components that are part of Mule Runtime. These components don’t connect to other systems, but instead help you to create the logic of a flow.

  1. Add a Choice router block (which consists of a Choice card and two or more subcards for the routing options) to the flow.

    1. Click the plus sign that is between the Set Variable card and the Twilio card.

    2. Select the Choice router.

      Result: The Choice router block appears in the flow.

      choice router card
  2. Set up the routing options.

    1. Click the first subcard to open it.

    2. Click the three dots in the subcard’s top-right corner and select Add Below. The current subcard closes and another opens beneath it.

    3. Click the first subcard.

    4. Paste payload.Amount > 100000 into the Expression field.

    5. Name the subcard Enterprise.

    6. Click the second subcard.

    7. Paste payload.Amount > 50000 into the Expression field.

    8. Name the subcard Small Business.

    Result: The Choice router block now has the three routing options that you need in your flow. The Default option is used for the Retail SKUs because it is for orders in the amount of $50,000 and below. Your flow now looks like this:


    flow with choice
  3. Query the product database according to whether the size of the deal is “Enterprise” or “Small Business”.

    1. Click the plus icon next to the Enterprise subcard.

    2. Add a Database card and choose the Select operation.

    3. Configure the database driver by using the following information:

      • Connection Type: MySQL Connection

      • Host: ntoproducts.c3w6upfzlwwe.us-west-1.rds.amazonaws.com

      • Port: 3306

      • User: mulesoft

      • Password: mulesoft

      • Database: nto_products

    4. Test the connection and click Save if it is successful.

    5. Add this query to the Database card:

      select * from products where category = "Enterprise" and inventory > 1 limit 1
    6. Close the Database card.

    7. Add a Database card after the Small Business subcard, and select the Select operation.

      Notice that you do not have to configure the database driver again. That’s because, when you configured it earlier, Flow Designer saved the configuration. If you look under the heading “Reusable Configurations” in the Projects sidebar, you’ll see an entry labelled “Database”.

    8. Add this query to this Database card:

      select * from products where category = "Small Business" and inventory > 1 limit 1
  4. Set up the default option for when the size of the deal is $50,000 or less. In this case, you do not need to query the database.

    1. Add a Set Payload card after the Default subcard. You use the Set Payload card here so that, if the Choice router block routes to this default option, the flow simply adds the required SKU and associated information.

    2. In the open Set Payload card, click the f(x) button to enter script mode in the Value field.

    3. Paste the following JSON data into the field:

         "productID": "CK111274",
         "category": "Retail",
         "SKU": "CKS0",
         "productName": "Old School Classic Kicks",
         "inventory": 9999

The flow should now look like this:

flow with complete choice

Change the format of the information before passing it to the NTO Order API

By this point, the flow has obtained information about the opportunity from Salesforce and about the product from the product database. Before using this information to place an order with the NTO Order API, the flow needs to format it in the way that the API is expecting.

  1. Click the plus sign to the right of the Choice router block and add a Transform card.

  2. In the Input pane, specify input data types.

    1. Click the three dots next to payload.

    2. Select Set.

    3. Select the Product Information data type that you created earlier.

    4. Click the three dots next to OppDetails under vars and set the data type to Salesforce Opportunity.

  3. In the Output pane, set the data type for payload to Order Information.

    Result: The Transform card should look like this:

    transform first after input output
  4. Drag and drop elements from the left side to right side according to the mappings below:

    This Element on the Left Side Maps to This Element on the Right Side

















If you see the message Output media type differs from expected one, click the Fix it link to resolve the problem.

When you are done changing the format, the Transform card looks like this:

transform first final

The JSON that the card displays in the Preview pane is an example of the JSON that the flow will send to the NTO Order API when placing an order.

Place an order using NTO Order API

Now the flow is ready to place an order. The flow can place it with the NTO Order API, which is a REST API, by sending a POST request. In response, the API will return the order ID, the tracking number for the order, and the estimated arrival time for the order at the customer’s location.

  1. Add an HTTP Request card on the right side of the Transform card you just configured.

  2. In the card, specify these details:

Transform data for the Twilio API

At this point, the NTO Order API has returned the order ID, the tracking number for the shipment of the order, and the ETA for the shipment at the customer’s location. The last step is to send this information to the Twilio API, which will then send an SMS with the details to the customer. For Twilio to accept this information, you need to format the message in the format that Twilio requires.

  1. Add a Transform card to the left of the Twilio card.

  2. In the Input section, set two data types:

    1. Set the data type for payload to Order Tracking.

    2. Set the data type for OppDetails to Salesforce Opportunity.

  3. In the Output section, follow these steps:

    1. Double-click the To field and add your phone number, together with the country prefix. (For example, the country prefix for the United States is 1, so a telephone number would look like this: 12223334444.)

    2. Double-click the From field and add the free phone number that you obtained when you opened your Twilio developer account.

  4. At the bottom of the Transform card, click the Script tab.

  5. Rather than map fields, create the transformation an alternative way. Paste the following DataWeave script into the position in the Transformation script panel that the screenshot shows:

    Body: "Order for " ++ vars.OppDetails.Name ++ " has been placed. Details:
    " ++ "OrderID: " ++ payload.OrderID as String ++ ", Tracking: " ++ payload.TrackingNo ++ ", ETA:" ++ payload.ETA as Date
    transform second script panel

    Using DataWeave scripts is another way of mapping input fields to output fields. Click the Mappings tab to see how the fields are mapped as the script specifies.

    transform second mappings panel

Result: The Preview pane shows an example of the JSON messages that the Transform card will send to the Twilio card.

Test your application

You are just about ready to test your Mule application. First, though, make sure there are no errors in the flow. If there are errors, you can open the Problems pane at the bottom of the canvas to read descriptions of them. Click an error to be taken to its location in the project.

If there are no errors and if the Run button is enabled in the top-right corner of Flow Designer, click Run to run the application. Then, follow these steps:

  1. Log in to your Salesforce instance using the credentials you used in the Salesforce card.

  2. In the top-left corner of your browser window, open the Sales app from the app switcher, which looks like this:

    salesforce app switcher
  3. Go to Opportunities and change the view to All Opportunities.

  4. Find an opportunity that is not in the Closed Won stage.

  5. Open and edit the opportunity by changing the value of Stage to Closed Won.

  6. Update the amount to be greater than $100,000, and then click Save.

  7. Go back to your Mule application in Flow Designer. If you see a green line in each card, the flow was triggered and a text message with the order details was sent to your mobile phone.

Nice job!

You’ve received your text message, run around the office showing it to everyone, and triggered the application several times to admiring audiences and many congratulatory remarks. You’ve replied with modesty, jovially and collegially explaining what Flow Designer is, pointing to the cards, explaining what connectors and components you use in the application, and indicating with your forefinger how the flow is triggered and how data moves through it. You’ve even received a thumbs-up for making your application available outside of the Design environment where you created it.

So, you are now ready for the very last step: deploying your Mule application. This tutorial doesn’t cover that, but the link takes you to help that shows the steps, if you want to deploy this example Mule application.

We hope this tutorial was easy enough to follow, yet complex enough to show you how you can use Flow Designer to your benefit in real-world scenarios. Happy flow-designing!

Was this article helpful?

💙 Thanks for your feedback!

Edit on GitHub
Submit your feedback!
Share your thoughts to help us build the best documentation experience for you!
Take our latest survey!