Over the past few weeks I have been writing tutorials, giving out code snippets, and data base schema's trying to help PHP developers, that work with Salesforce, some idea on how to build a local database replication system so that an organization can use their salesforce data with their own systems. Today I am releasing the first of a series of scripts that can be used as is or modified that will create a copy of your data in a MySQL database. I am starting this series on the Salesforce Account Object / Table.
I have successfully created PHP scripts which export my organizations data from salesforce into Oracle 10g. I know that many organizations use MySQL (since it is open source) and very stable. I have not seen any examples of exporting data from Salesforce into MySQL so I thought I would try and help out a little.
Attached is a schema for the account object / table for MySQL, also attached is complete working export script that will export the account object from salesforce and store it in MySQL, given the schema that I am providing. To run this script you have to have the following:
- PHP 5
- ADOdb database abstraction layer / class
- Your salesforce username and password
- MySQL connection strings (password, user, and database name)
In this example you have to change line 14 of the script with your MySQL account settings:
PHP Code:
$db->Connect("localhost", "Username", "password", "TargetDatabaseName");
I also added a new part to the script that allows for the truncation of the local MySQL table. I use this because the data coming in is really a data dump and this will also eliminate duplicates in your data. Someone asked me why I also do not set a Primary key on this or other tables when working with salesforce. Well if you try and set such a key, let's say on the Id field and you try and run this script on a weekly basis without a truncate, you will not be able to insert data into the table because it will error on inserting on a primary key. I have tested this and it will error, I assure you.
You can comment out the code if you wish, look around line 19-21:
PHP Code:
$sql = "TRUNCATE TABLE sforce_account";
if ($db->Execute($sql))
echo "Salesforce Account Table Truncated
";
In order to get this account table replication working, I suggest this:
Open the account SQL file and import it into your database or test database using phpmyadmin or some other data base tool.
Open and edit the attached PHP script and change the MySQL database settings and your salesforce connection (user and pass)
Check the paths in the PHP script (see line 6 - 8):
PHP Code:
require_once ('./includes/soapclient/SforcePartnerClient.php');
require_once ('./includes/soapclient/SforceHeaderOptions.php');
require_once ('adodb.inc.php');
Then just run the script and once it is complete, compare the number of records you have in your instance of Salesforce.com to the record count of MySQL:
Code:
SELECT count( * ) FROM sforce_account
The script already performs a count of your accounts (see line 37):
PHP Code:
echo 'There are currently ' . count($accounts) . ' accounts:';
I hope this works for you and if you have any questions, please feel free to contact me
Thanks,
Mike
Please remember that while this and other scripts on my site work in my environment, they are just examples. Use of these scripts and tutorials are at the risk of whomever downloads and uses them as is I assume no risk what so ever.