Salesforce to MySQL Database using Batch Processing
home
Illustrates how to use batch processing to synchronize Salesforce information with a database.
Batch Processing allows you to split a payload into individual elements to process each individually. This functionality is particularly useful when working with streaming input or when engineering "near real-time" data integration between SaaS applications.
Example
This application queries a Salesforce account for new or updated contacts at a regular interval, then processes the returned payload one record at a time. It checks to see if a contact currently exists in the database, then updates or creates a new contact accordingly. Once the process is complete for the entire batch, a success message is logged.
Although this example could be met without using batch processing – treating the entire list of contacts returned by Salesforce as a whole – using batch makes this process more reliable as any errors that occur in a single record do not propagate beyond record level.
Set Up and Run the Example
Complete the following procedure to create, then run this example in your own instance of Anypoint Studio. You can create template applications straight out of the box in Anypoint Studio and tweak the configurations of this use case-based template to create your own customized applications in Mule.
Skip ahead to the next section if you prefer to simply examine this example via code snippets.
- Create the example application in Anypoint Studio.
- Set up Salesforce credentials:
- Log in to your Salesforce account. From your account menu (your account is labeled with your name), select Setup.
- In the left navigation bar, under the Personal Setup heading, click to expand the My Personal Information folder.
- Click Reset My Security Token. Salesforce resets the token and emails you the new one.
- Access the email that Salesforce sent and copy the new token onto your local clipboard.
- In the package explorer, open src/main/resources/connector.properties
- Complete the file with your own username, password, and security token.
- Create a Database and set up credentials:
Create a new MySQL Database
Create a new MySQL Database
If you do not have a MySQL database available for your use, you can install MySQL on your local computer. Please visit dev.MySQL.com to download and install a free version. It is a good idea to also install the MySQL workbench. Please also configure a MySQL username and password for use with this project.
- The project requires the following database configuration:
- MySQL Database Schema: SFtoDB_Example
- One table: contact
- Four fields: email, first_name, last_name, last_modified
- One or more rows of data should be inserted into the table
- A user that must have read and write access to this data.
You can execute the following SQL statement to produce this schema and populate one row of data. See the green box below for tips on executing this SQL.
USE SFtoDB_Example; CREATE TABLE contact ( ID INT(11) NOT NULL AUTO_INCREMENT, email varchar(255) NOT NULL, first_name varchar(255) DEFAULT NULL, last_name varchar(255) DEFAULT NULL, last_modified varchar(255) NOT NULL, PRIMARY KEY (email) ); INSERT INTO contacts VALUES (NULL, "leonardmule@mulesoft.com", "Leonard", "Mule", "");
If you are using the MySQL Workbench, first create the schema SFtoDB_Example, and execute the above SQL statements from within the MySQL Workbench SQL Editor.
Alternatively, you can use the MySQL command-line tool, as follows:
- Execute this command: CREATE DATABASE SFtoDB_Example;. This creates the database schema.
- Save the above SQL code-block to a text file, SQL_for_example.sql
Then execute the SQL with this command: mysql SFtoDB_Example < SQL_for_example.sql
In either case, you may need to assign a user for access to the schema as well. See MySQL Documentation for more information on using the MySQL Workbench or the command-line tool.
- In the package explorer, open src/main/resources/connector.properties
- Complete the file with your own DB credentials
- 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.