Contact Us 1-800-596-4880

Connect with Salesforce Example

Mule Runtime Engine versions 3.5, 3.6, and 3.7 reached End of Life on or before January 25, 2020. For more information, contact your Customer Success Manager to determine how you can migrate to the latest Mule version.

Enterprise, CloudHub

This application uses pre-packaged tools to intelligently connect with Salesforce. Based on a simple use case, the application takes a CSV file of contacts and uploads the contact information to an active Salesforce user account. It uses DataSense and the Anypoint DataMapper Transformer to map and transform data, thereby facilitating quick integration with this Software as a Service (SaaS) provider.

Connect with Salesforce

csv_to_sfdc2

At times, you may find that you need to connect one or more of your organization’s on-premises systems with a SaaS such as Salesforce. Ideally, these independent systems would talk to each other and share data to enable automation of end-to-end business processes. Use Mule applications to facilitate communication between your on-prem system(s) and Salesforce. (Though this use case does not extend as far, you can also use Mule to facilitate communication between SaaS providers.)

Assumptions

This document assumes that you are familiar with Mule and the Anypoint Studio interface. To increase your familiarity with Studio, consider completing one or more Anypoint Studio Tutorials. Further, this example assumes that you have a basic understanding of Mule flows, Mule Global Elements, and Studio’s Anypoint DataMapper.

This document describes the details of the example within the context of Anypoint Studio, Mule ESB’s graphical user interface, and includes configuration details for XML Editor where relevant.

Example Use Case

Though a simple example, this application nonetheless employs complex functionality to demonstrate a basic use case. The application accepts CSV files which contain contact information – name, phone number, email – and uploads them into a Salesforce account, automatically inserting the correct data into each Salesforce field.

contacts-to-SFDC

Set Up and Run the Example

Complete the following procedure to create, then run this example in your own instance of Anypoint Studio. Skip ahead to the How it Works if you prefer to simply examine this example via screenshots and code snippets.

To witness end-to-end functionality, you must have an active Salesforce account into which you don’t mind inserting three sample contacts. After this application automatically uploads these contacts, you can manually delete them in your Salesforce account.
  1. Create the example application in Anypoint Studio. Do not run the application.

  2. Log in to your Salesforce account. From your account menu (your account is labeled with your name), select Setup.

  3. In the left navigation bar, under the Personal Setup heading, click to expand the My Personal Information folder.

  4. Click Reset My Security Token. Salesforce resets the token and emails you the new one.

  5. Access the email that Salesforce sent and copy the new token onto your local clipboard.

  6. In your application in Studio, click the Global Elements tab.

  7. Double-click the Salesforce global element to open its Global Element Properties panel. In the Security Token field, paste the new Salesforce token you copied from the email. Alternatively, configure the global element in the XML Editor.

Configure Global Element

Studio Visual Editor

sfdc_global

Studio XML Editor

<sfdc:config name="Salesforce" username="testsfdc@mulesoft.com" password="password"  doc:name="Salesforce" securityToken="bgfsG5688kroeemIHMnYJ">
  <sfdc:connection-pooling-profile initialisationPolicy="INITIALISE_ONE" exhaustedAction="WHEN_EXHAUSTED_GROW"/>
</sfdc:config>

Configure Username and Password

  1. Change the contents of the Username and Password fields to your account-specific values, then click OK to save your changes.

  2. Back in the connector’s Properties Editor, set the Operation to 'Create' and the sObject Type to ' Contact ':

    select_operation

Run the Application

  1. In the Package Explorer, right-click the connect-with-salesforce project name, then select Run As > Mule Application. Studio runs the application on the embedded server.

  2. This project includes a sample CSV file, called contacts.csv, that you can use to witness end-to-end functionality of the application. In the Package Explorer, click the src/test/resources folder to expand it, then find the contacts.csv file inside this folder.

  3. Click and drag the contacts.csv file into the input folder in the same directory.

  4. The File connector in the application polls the input folder every ten seconds. It picks up the CSV file, processes it, then deposits it into the output folder in the same directory. (Hit F5 to refresh the contents of the input and output folders.)

  5. In your browser, access your Salesforce account, then navigate to the Contacts tab.

  6. Use the drop-down menu to display All Contacts, then scan your contacts for three new entries:

    • Charles Bingley

    • Fitzwilliam Darcy

    • George Wickham

  7. Stop the Mule application by clicking the square, red terminate button in the Console.

  8. Delete the three sample contacts from your Salesforce account.

How it Works

Using a single flow with three elements, this application accepts CSV files which contain contact information, then uploads the contacts to Salesforce.

The File connector Polls the input folder for new files every ten seconds. When it spots a new file, it reads it and passes the content to the DataMapper Transformer. This transformer not only converts the format of the data from CSV to a collection, it automatically maps the input fields from the CSV file – FirstName, LastName, etc. – to output fields that Salesforce uses in a collection.

Each mapping earns an arrow which helps you to visualize the activity that occurs within the DataMapper transformer. When it has converted all the contacts in the file to a collection of Salesforce-friendly data, the application uses a Salesforce Connector to push data into your Salesforce account. The connector’s configurations specify the operationCreate – and the sObject typeContact – which dictate exactly how the data uploads to Salesforce; in this case, it creates new contacts.

While the application’s functionality is relatively straightforward, the beauty of this project is illustrated through its use of DataSense. Rather than building the application serially – adding, then configuring each of the elements manually according to the order in which they appear in the flow– you can use DataSense to complete the most difficult configurations automatically. The following steps outline the process to build this application.

  1. Drop a File connector into your application, completing the simple configuration to enable it poll a specific folder for input files.

Configuring the File Connector

Studio Visual Editor

filesalesforce

Field

Value

Display Name

File Input

Path

src/test/resources/input

Move to Directory

src/test/resources/output

Polling Frequency

10000

Studio XML Editor

<file:inbound-endpoint path="src/test/resources/input" moveToDirectory="src/test/resources/output" pollingFrequency="10000" responseTimeout="10000" doc:name="File Input"/>

Attribute

Value

path

src/test/resources/input

moveToDirectory

src/test/resources/output

pollingFrequency

10000

doc:name

File Input

Add a Salesforce Connector

Add a Salesforce Connector to the flow. At this point, you can configure the connector with your Salesforce account-specific details and test the connection to Salesforce. Not only does the embedded DataSense functionality confirm that you have a clear channel for communication, it gathers metadata about Salesforce objects and the type of data it accepts. (The value of this metadata becomes apparent with the introduction of a DataMapper into the flow further in this procedure.)

Configuring the Connector in Studio

Studio Visual Editor

  1. Modify the display name for the connector, if you wish, then click the plus sign next to the Config Reference drop-down to create a new Global Element.

  2. Select the Salesforce global element, then click OK.

  3. Enter values in the Username, Password and Security token fields, then click OK. (See the Set Up section above for details on how to acquire the security token.) Notice that Studio automatically enables DataSense in the global element.

sfdc_global

Studio XML Editor

<sfdc:config name="Salesforce" username="testsfdc@mulesoft.com" password="password"  doc:name="Salesforce" securityToken="bgfsG5688kroeemIHMnYJ">
  <sfdc:connection-pooling-profile initialisationPolicy="INITIALISE_ONE" exhaustedAction="WHEN_EXHAUSTED_GROW"/>
</sfdc:config>
<flow>
  ...
</flow>

Testing the Connection

When you click Test Connection, Mule tests the connection to Salesforce (see image below). With a valid username, password and security token, the connection test results in success and Mule saves your global element configurations. If any of the values are invalid, the connection test results in failure, and Mule does not save the global element, prompting you to correct the invalid configurations.

test_connection2

Selecting Operation and sObject

Back in the Salesforce connector properties editor, use the drop-down menus to select the Operation and sObject Type. Because the DataSense activity has gathered metadata about Salesforce’s operations and data sObject types, Mule is able to present a list of Salesforce-specific values in the drop-down menus for each of these fields (see image below).

select_operation
Field Value

Operation

Create

sObject Type

Contact

Adding DataMapper

Having defined the Salesforce-friendly output, you can then drop a DataMapper between the elements in the flow to map CSV input fields to Salesforce output fields. Because DataSense has already acquired the operation and sObject information from Salesforce, the DataMapper demands that you configure only the input values (below, left). In this example application, we used an existing CSV example to define the input fields in DataMapper (below, right).

dataMapper_mapping
  1. Click the edit icon next to Type in the Input panel to change the input type to CSV.

  2. Use the radio buttons to select User Defined, the click Create/Edit Structure…​

    adjust_DM
  3. Define the fields in the CSV file from which DataMapper draws its input values. Click OK.

    define_csv
  4. When you click Create mapping, Mule maps input fields to output. Where the input and output fields have identical names, DataMapper intelligently, and automatically, maps input to output, as with the fields in this example application. Otherwise, you can quickly map input to output manually by clicking and dragging input fields to output fields in the Data Mapping Console (see below).

    mapped_data
  5. The configuration now complete, you can save, then run the application. Feed CSV files with contact information into the input folder, and watch the new contents appear in your Salesforce account (see image below).

    sfdc_contact_list

Full Code

Studio Visual Editor

flow_contacts_to_sfdc

Studio XML Editor

<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:data-mapper="http://www.mulesoft.org/schema/mule/ee/data-mapper" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation" xmlns:file="http://www.mulesoft.org/schema/mule/file" xmlns:sfdc="http://www.mulesoft.org/schema/mule/sfdc" xmlns:spring="http://www.springframework.org/schema/beans" xmlns:tracking="http://www.mulesoft.org/schema/mule/ee/tracking" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.mulesoft.org/schema/mule/file http://www.mulesoft.org/schema/mule/file/current/mule-file.xsd
http://www.mulesoft.org/schema/mule/ee/data-mapper http://www.mulesoft.org/schema/mule/ee/data-mapper/current/mule-data-mapper.xsd
http://www.mulesoft.org/schema/mule/sfdc http://www.mulesoft.org/schema/mule/sfdc/current/mule-sfdc.xsd
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/ee/tracking http://www.mulesoft.org/schema/mule/ee/tracking/current/mule-tracking-ee.xsd">
    <sfdc:config doc:name="Salesforce" name="Salesforce" password="password" username="salesforceuser@email.com">
        <sfdc:connection-pooling-profile exhaustedAction="WHEN_EXHAUSTED_GROW" initialisationPolicy="INITIALISE_ONE"/>
    </sfdc:config>
    <data-mapper:config doc:name="DataMapper" name="datamapper_grf" transformationGraphPath="datamapper.grf"/>
    <flow doc:description="Upload a csv file of contact information into Salesforce as new contacts." doc:name="Contacts_to_SFDC" name="Contacts_to_SFDC">
        <file:inbound-endpoint doc:name="File Input" moveToDirectory="src/test/resources/output" path="src/test/resources/input" pollingFrequency="10000" responseTimeout="10000"/>
        <data-mapper:transform config-ref="datamapper_grf" doc:name="DataMapper"/>
        <sfdc:create config-ref="Salesforce" doc:name="Salesforce" type="Contact">
            <sfdc:objects ref="#[payload]"/>
        </sfdc:create>
    </flow>
</mule>

Documentation

Studio includes a feature that enables you to easily export all the documentation you have recorded for your project. Whenever you want to share your project with others outside the Studio environment, you can export the project’s documentation to print, email or share online. Studio’s auto-generated documentation includes:

  • A visual diagram of the flows in your application

  • The XML configuration which corresponds to each flow in your application

  • The text you entered in the Notes tab of any building block in your flow

Follow the procedure to export auto-generated Studio documentation.