Python can make managing Salesforce data a breeze. In this post, I’ll show you how to get connected, retrieve data from salesforce, and then load it into a Pandas dataframe.
To start, import the Simple Salesforce python package
pip install simple-salesforce
Next, create a connection to Salesforce.
from simple_salesforce import Salesforce
sf = Salesforce(username='myemail@example.com', password='password', security_token='token')
I highly recommend storing your username, password, and security token as environment variables. That way you are not displaying your credentials openly in your code. Check out my post on Storing your Passwords in your Bash Profile.
If you don’t know your security token, here is how to get it. Click on the mascot icon in the top right > Settings
Next, click on the “Reset My Security Token” in the left panel and then “Reset Security Token”
Once you do that, your token will be emailed to you.
GET Requests
The syntax is pretty basic. following client.sobjects
, you’ll put the target Object followed by the Salesforce ID
sf.Opportunity.get('0063h000008yWZuAAM')
To find the API name of any Object, click on the Gear Icon > Setup in the top right corner of. Then click on the Object Manager Tab. Now you’ll see a list of all your objects with the respective API name.
The Salesforce ID can be found in the url when you are looking at the respective record. In the screenshot below, I am on a specific opportunity record page.
However, going to the respective page to pull the ID kind of defeats the purpose of the API. In the next section, I’ll show you how to query Salesforce for the ID.
Querying
Querying salesforce is the same thing as making a report. Here’s the basic syntax:
query_results = sf.query('''
SELECT Id, FirstName, LastName
FROM Contact
WHERE FirstName='Felix'
''')
The triple single quotes '''
allows you to have your string carry over into multiple lines.
You can also play around with querying directly in Salesforce by clicking on the Gear Icon > Developer Console > Query Editor (near the bottom)
Convert data to a Pandas Dataframe
Run the following code to convert the JSON data that was returned to a Pandas dataframe.
df = pd.DataFrame(query_results['records']).drop(columns='attributes')
There are some extra columns that get created in the process, so you can filter those out like this:
df = df[['Id','FirstName','LastName']]
Now you are ready to analyze the data in Pandas!