Saturday, November 20, 2010
OK, guys I am back to blogging after long time.
This blog is about synchronizing Magento inventory with Saleforce. I have briefly mentioned what is saleforce in my previous blog. You can read it from here.
The requirement was to sync Magento Inventory to Salefroce. For the first time it should synchronize all the products form Magento to Salesforce and then it should synchronize only recently added and updated products to Magento. First of all we need to understand how we can have products in Saleforce. This information is missing in my previous blog so I am explaining here.
In Saleforce you can expose your organization data in form of Custom Objects. Its like your table in your database. When you define a custom object, in salesforce it defines a table. All the attributes of that object can be defined as column of that table. For example let's consider custom object product with few attributes like Product name, quantity etc. You can imagine that Force.com platform has created a table called product and with columns product name and quantity.
Once you have defined a custom object in Salesforce you can add number of products in it. So its like you have a table called product and you have number of rows in it. This is how you can define custom objects in Salesforce. Salesforce custom objects supports various data types like NUMBER, TEXT, LIST, MULTI SELECT, FORMULA and various other. You can enforce security on it by providing crate,read,modify access to group of users or specific user.
So this was the introduction about saleforce custom object, now lets go further.
Salesforce has some APIs using which third party can access the data inside the Salesforce platform. We have used this APIs in Magento. Those APIs are nothing but a web services. You have to download WSDL file from your salesforce account and using it you can connect to salesforce from third party.
For PHP toolkit is available that you can download from SalesForce. It defines all the classes and interface required to access salesforce data form PHP applications. You have to include some files from this toolkit and wsdl file into your PHP application. Following is the simple code snippet to authenticate user to access salesforce data.
$wsdl = 'your wsdl.wsdl';
$user = 'your username';
$pass = 'your password';
$token = 'security token';
$client = new SforcePartnerClient();
$client->login($user, $pass . $token);
if($client->login($user, $pass . $token))
echo "Connection Succcess";
Here security you have to generate from your salesforce account and username and password is of your salesforce account.
After successful authentication you can retrieve list of custom objects from salesfroce or you can add,update,delete custom objects in salesforce. For that various methods are defined in toolkit but we will focus on method UPSERT. This is the combination of insert and update. When you use this method, salesforce will update object its available or it will create new custom object.
For products in Magento we have to map each product attributes to custom object fields. Look at the following code snippet.
$sObject = new sObject();
$sObject->type = 'TDS__Product__c';
$sObject->fields = $fieldset;
Here important field is External_Product_ID__c. Its like foreign key. When you want to access salesforce custom object from third party, you have to specify some field as a external which must have datatype Text,Auto number.
Using above code it will either update custom object in Salesforce with specified values if its available or it will create new one.
So this is how you can synchronize Magento products to salesforce. I hope that this blog helps you. Let me know if you have any questions for the same.