Get customer details from Salesforce Connector

This use case polls an email to get Account Name from the subject and calls Salesforce to get the account details. These details are converted to HTML format and sent back to the caller. It also records the Action and Response into the database and writes the response to a file.

At the end of this tutorial, you will be able to

  1. Read emails via IMAP and prepare your payload to access your Salesforce account
  2. Handling Exceptions
  3. Configure Salesforce connector and retrieve customers belonging to specific account
  4. Notifying the Caller when no records are returned

For processing, the response into File system and Database go to link and see the third section mentioned Process Email details to File system and Database.

Before we start the tutorial, we would need the below pre-requisites to be in place.

Please refer link and see pre-requisites section on the top.

One more pre-requisite required for this use case. You need to have a Salesforce account to get started with this use case. To use the Salesforce connector, you need:

1.Salesforce developer account– Sign up for one at link:

2.Security token– Sign into link:, click your name in the upper right corner, then click Setup > My Personal Information > Reset Security Token. Then, click Reset My Security Token. Salesforce sends your security token via email to your registered email address.

3.Consumer key and Secret– If you are using the Salesforce connector to access an OAuth API, you also need a consumer key and secret. Refer to the detailed documentation on how to use the Salesforce connector to access an OAuth API

1. Read emails via IMAP and prepare your payload to access Salesforce account

1. create new mule project

2. Enter the IMAP and SMTP details for GMAIL into the file present in src/main/app path. write your details in the properties file like below.

Make sure to enter your test email id and password (you created in the pre-requisites step) for IMAP.USERNAME and IMAP.PASSWORD properties respectively.

3. Mule flows are present under the src/main/app folder.

As we are already aware, we should reply to the caller with the customer details from Salesforce Let’s create a sub flow for the same in advance so that we could use it in other flows.  Add a logger into the flow to indicate we have reached this sub flow.

Add SMTP connector and configure it as follows


4. Open the salesforceconnector.xml. From the Mule Palette drag the IMAP connector. Follow below steps to configure the IMAP inbound endpoint.

1. Click on IMAP connector then, In the General tab, Basic settings, observe that we are getting the value for Host, Port, Username, and Password from the file (from the previous step). Refer below; Figure 1: General Settings. Click on Security tab and enable IMAPS. (Refer Figure 2: Security tab )

Figure 1: General settings

Figure 2: Security tab

2. Click on the ‘+’ icon beside connector configuration. Enter your mailbox folder (Refer Figure 3: Connector Configuration – General). Go to Security tab and enable IMAPS. (Refer Figure 4: Connector Configuration – Security). Click OK.

 Figure 3: Connector Configuration – General

Figure 4: Connector Configuration – Security

5. Create 2 new session variables after IMAP connector to store from address and subject.

6. We will use choice component to check and validate the email subject. For this use case, we expect to receive a mail with the subject “Get Account <Account-Name>”.

In Salesforce you will find an account named sForce. If you must retrieve the customers for sForce you would send a mail with subject Get Account sForce. In the coming steps, we will be validating the same using choice in the mule.

1. Create a new sub-flow called salesforce_connectorFlow. Let it be empty for now we will be adding required message processors soon.

2. Add the below choice component after the two session variables that we just created.


3. As you see we check if the subject contains Get Account. If so we route to the currently empty salesforce_connectorFlow after setting the payload with the Account Name. In our case, it would be sForce.

4.Else if we receive invalid subject, we call the send_response sub-flow after setting the payload as ‘Invalid Request’

5.Below is how checkForEmailSubject flow would look like till this stage


2. Exception Handling

1. Drag a Catch Exception Strategy in the exception part of checkForEmailSubject flow

2. Add a logger to log the error message

3. Set the payload with the error message and payload at the time error occurred.

4.Add a flow reference to send_response flow so that the sender is notified that the subject was invalid

Below is the configuration XML part for catch exception strategy

Below is how the flow looks with exceptions being handled.

Excellent!!! As of now we have created a flow which receives incoming emails, checks if the subject is valid and proceeds to the salesforce_connectorFlow

If the subject is invalid a mail is sent back to the caller that the subject was invalid.

3. Configuring Salesforce connector and retrieving customer details

In the previous section, we created an empty sub-flow salesforce_connectorFlow. Let’s start adding message processors onto the flow

1. Add a logger component to the flow and log the payload. Then,  Drag Salesforce connector into the flow.

2. Click on Salesforce. In General tab-> Basic Settings-> Connector Configuration, click on the plus (+)icon.

3. Click on Salesforce Basic Authentication and click OK

4. Remember in pre-requisites step we created a Salesforce account and you would have the username, password and security token. Enter the details in the next screen.

Click on validate Configuration and you will be prompted that the connection was successful

5.Configure the Salesforce adapter as shown below

Note that we call this sub-flow from checkForEmailSubject flow and we would have sent the account name in the payload. The same is used to query Salesforce.

As of now, our mule sub-flow would look as below

6.Transform Salesforce response to send the email to the caller.

1. Add a weave transform message component into the flow after Salesforce connector.

2. Checking its properties, this is how it would look currently.

3. You can see that the transform component is aware of the response from Salesforce and you see a list of Email, Id and Name at the left side (input side). These were the 3 field we queried on Salesforce.

4. At the output side of transform message properties click on ‘Define Metadata’

5. A window pops up where you define a user defined datatype.Click on

6. Enter type id and click on create type

7.Select type as XML. And use the below XML code and make a XML (e.g., slaesforce_sample.xml) file

8. You see that the schema is automatically generated using the example file

9. At this stage, this is how the transform message properties would look like.You see on the output side, we have elements to map. Drag the email from input to the output side. Do the same for Id and Name

We have now completed the ‘Transform Message’ below is the flow.


4. Notifying the Caller when no records are returned

1. Create a flow variable after Transform Message to store the payload

2. Create a new sub-flow named processRecords. Let it be empty as of now we will be adding message processors to it shortly
3. Add a choice component within salesforce_connectorFlow flow after set-variable

Carefully examine the when expression. You see that we check the XML to know if it has the NODE ‘Account_Details’. This tells us that Salesforce has returned data.

1. If the condition is true meaning we have received customer details from Salesforce, we proceed to the processRecords sub-flow after assigning the payload with the XML String.

2. Else We send a response back to the caller saying, ‘No Records Found’

We have now completed the salesforce_connectorFlow sub-flow

As I mentioned earlier, for processing the response into File system and Database go to link and see the third section mentioned Process Email details to File system and Database.

configuration XML

Complete code can be downloaded from below link,

Testing the Flow

  1. Right Click on the project and Run As Mule application. Wait till the application starts up.
  2. Login to your email. Compose a new email with subject ‘Get Account sForce’ and send it to the email-id that is configured in your

    Click Send and go over your logs in Anypoint studio
  3. Now let’s check the filesystem to see if the new file is created.
  4. Let’s check on the database table.

We see that the response is successfully recorded

  1. Now for the last main aspect, Response should be sent back to the caller

Yes!! We have received the employee details as expected.

Lessons learned from this use case:

– Understanding of how Salesforce works and Salesforce connector
– Working with Salesforce connector
– Exception handling

We are at the end of this use-case. Thanks for your time. Hope it was worth it.