Python Integration with Salesforce (Windows)

Thursday, January 28, 2010 by Aslam - The Alexendra
Hi,

Recently I was challenged to access Salesforce from a Python script. Like any Python beginner I had some problems setting the Python environment up correctly and then connecting to Salesforce. However, after much trial and error I succeeded, so I am here to share my Python-Salesforce journey. Lets walk through it together :)

Step 1: Get the latest Python code (I used Python2.6) and install it on your disk at "c:\Python26"

Step 2: Download the suds from https://fedorahosted.org/suds Extract suds in a folder such as "C:\Python26\suds"

Step 3: Download the latest setuptools for your python(2.6) from here http://pypi.python.org/pypi/setuptools#downloads. Choose the "setuptools-0.6c11.win32-py2.6.exe (md5)" for windows and install it in your python directory (Default C:\Python26)

Step 4: Open the command prompt, go to the suds directory and issue this command:-

c:\Python26\suds> C:\Python26\python.exe ./setup.py install

Suds will install successfully

Step 5: Now install Apache or use your existing apache server if you have one. The only important thing here is the httpd.conf file. So do the following main changes in your apache httpd.conf file:-

a) Find the line "Options Indexes FollowSymLinks" and replace it with

Options Indexes FollowSymLinks +ExecCGI


b) Find the line "AddHandler cgi-script" and replace it with

AddHandler cgi-script .cgi .py


I think that's it. If you still have issues setting up your Apache server properly then email me and I'll be happy to provide you my httpd.conf.

Step 5: Now start your Apache server and test a simple python script. Create a file test.py and put it on your server such as at "C:\wamp\www".

-------------------------------------------------------------
#!c:/Python26/python.exe -u
import datetime
import re
import string
import sys
import unittest
import logging
import test_config


logging.basicConfig(level=logging.INFO)
logging.getLogger('suds.client').setLevel(logging.DEBUG)



print "Content-type: text/html"
print

print "Hello1"


------------------------------------------------------------


open browser and hit the url like
http://localhost/test.py

Hurray!!! you have seen "Hello1". (If not :(, try again being careful with each step)


Step 6: Download your Enterprise WSDL from your Salesforce organization and put it on your server folder where you can access it easily

http://localhost/pythontest/enterprise.wsdl.xml

Step 7: Download "salesforce-python-toolkit" from http://code.google.com/p/salesforce-python-toolkit/downloads/list .

Extract it to a folder like "sforce". You must put it in your classpath for python. However, as I was new I didn't spend much time on this and for the sake of expediency I put this "sforce" folder into "C:\Python26\Lib".

Open the test_config.py file from this folder and put your organization's Username and Password properly. Save this file.

Open your test.py file again and now let's perform some basic query examples:

---------------------------------------------------------------------------

#!c:/Python26/python.exe -u
import datetime
import re
import string
import sys
import unittest
import logging
import test_config


logging.basicConfig(level=logging.INFO)
logging.getLogger('suds.client').setLevel(logging.DEBUG)

from sforce.enterprise import SforceEnterpriseClient

from suds import WebFault

print "Content-type: text/html"
print

h = SforceEnterpriseClient('http://localhost/pythontest/enterprise.wsdl.xml')
h.login(test_config.USERNAME, test_config.PASSWORD, test_config.TOKEN)

result = h.queryAll('SELECT Account.Name, FirstName, LastName FROM Contact LIMIT 2')
for record in result.records:
print record.FirstName, record.LastName
print record.Account.Name

result = h.logout()
---------------------------------------------------------------------------------------

In the above example, we queryed the Contact object. The important thing to note here is that the enterprise wsdl xml is referenced in your script. Python accepts the filename in full url format, so please specify it as either http:// or file:/// format.

Open browser and run the code. The results will appear on your screen.

Hope you enjoyed our little journey....

For any further questions email me aslam.bari@gmail.com


Thanks
Aslam Bari
Posted in | 2 Comments »

2 comments:

Anonymous said...

Does Salesforce Integrate with Windows and Plone easily?
You can email me at plerner@chronicletech.com

Nikunj Jani said...

What if I want to integrate salesforce with Ubuntu Linux ,can you guide me ?

Post a Comment