Querying Salesforce Orgs with Python
To access Salesforce orgs with Python, use the python module simple-salesfoce
: documentation, GitHub, library page
Install with: pip install simple_salesforce
Salesforce credentials are also required:
- Password
- Security Token
Security tokens are accessible from within the Salesforce org. Go to Setting » My Personal Information » Reset my Security Token.
from simple_salesforce import Salesforce
sf = Salesforce(username='',
password='',
security_token='')
Queries for Salesforce Objects are written as Salesforce Object Query Language (SOQL).
records = sf.query("SELECT Id, Name, Title, Email, Phone FROM Contact")
Now, records
contains a nested OrderedDict with the requested records.
To be useful for analytics, the records['records']
object can be converted to a Pandas DataFrame.
import pandas as pd
contacts = (pd.DataFrame(records['records'])
.drop(columns=['attributes']))
contacts
Id | Name | Title | Phone | ||
---|---|---|---|---|---|
0 | 0033t0000356R8wAAE | Stella Pavlova | SVP, Production | spavlova@uog.com | (212) 842-5500 |
1 | 0033t0000356R8xAAE | Lauren Boyle | SVP, Technology | lboyle@uog.com | (212) 842-5500 |
2 | 0033t0000356R8yAAE | Babara Levy | SVP, Operations | b.levy@expressl&t.net | (503) 421-7800 |
3 | 0033t0000356R8zAAE | Josh Davis | Director, Warehouse Mgmt | j.davis@expressl&t.net | (503) 421-7800 |
4 | 0033t0000356R90AAE | Jane Grey | Dean of Administration | jane_gray@uoa.edu | (520) 773-9050 |
5 | 0033t0000356R8pAAE | Rose Gonzalez | SVP, Procurement | rose@edge.com | (512) 757-6000 |
6 | 0033t0000356R8qAAE | Sean Forbes | CFO | sean@edge.com | (512) 757-6000 |
7 | 0033t0000356R8rAAE | Jack Rogers | VP, Facilities | jrogers@burlington.com | (336) 222-7000 |
8 | 0033t0000356R8sAAE | Pat Stumuller | SVP, Administration and Finance | pat@pyramid.net | (014) 427-4427 |
9 | 0033t0000356R91AAE | Arthur Song | CEO | asong@uog.com | (212) 842-5500 |
10 | 0033t0000356R8tAAE | Andy Young | SVP, Operations | a_young@dickenson.com | (785) 241-6200 |
11 | 0033t0000356R8uAAE | Tim Barr | SVP, Administration and Finance | barr_tim@grandhotels.com | (312) 596-1000 |
12 | 0033t0000356R8vAAE | John Bond | VP, Facilities | bond_john@grandhotels.com | (312) 596-1000 |
13 | 0033t0000356R92AAE | Ashley James | VP, Finance | ajames@uog.com | +44 191 4956203 |
14 | 0033t0000356R94AAE | Liz D'Cruz | VP, Production | ldcruz@uog.com | (650) 450-8810 |
15 | 0033t0000356R95AAE | Edna Frank | VP, Technology | efrank@genepoint.com | (650) 867-3450 |
16 | 0033t0000356R96AAE | Avi Green | CFO | agreen@uog.com | (212) 842-5500 |
17 | 0033t0000356R97AAE | Siddartha Nedaerk | None | None | None |
18 | 0033t0000356R98AAE | Jake Llorrac | None | None | None |
19 | 0033t0000356R93AAE | Tom Ripley | Regional General Manager | tripley@uog.com | (650) 450-8810 |
The full nested OrderedDict of records
is printed below.
records
OrderedDict([('totalSize', 20),
('done', True),
('records',
[OrderedDict([('attributes',
OrderedDict([('type', 'Contact'),
('url',
'/services/data/v42.0/sobjects/Contact/0033t0000356R8wAAE')])),
('Id', '0033t0000356R8wAAE'),
('Name', 'Stella Pavlova'),
('Title', 'SVP, Production'),
('Email', 'spavlova@uog.com'),
('Phone', '(212) 842-5500')]),
OrderedDict([('attributes',
OrderedDict([('type', 'Contact'),
('url',
'/services/data/v42.0/sobjects/Contact/0033t0000356R8xAAE')])),
('Id', '0033t0000356R8xAAE'),
('Name', 'Lauren Boyle'),
('Title', 'SVP, Technology'),
('Email', 'lboyle@uog.com'),
('Phone', '(212) 842-5500')]),
OrderedDict([('attributes',
OrderedDict([('type', 'Contact'),
('url',
'/services/data/v42.0/sobjects/Contact/0033t0000356R8yAAE')])),
('Id', '0033t0000356R8yAAE'),
('Name', 'Babara Levy'),
('Title', 'SVP, Operations'),
('Email', 'b.levy@expressl&t.net'),
('Phone', '(503) 421-7800')]),
OrderedDict([('attributes',
OrderedDict([('type', 'Contact'),
('url',
'/services/data/v42.0/sobjects/Contact/0033t0000356R8zAAE')])),
('Id', '0033t0000356R8zAAE'),
('Name', 'Josh Davis'),
('Title', 'Director, Warehouse Mgmt'),
('Email', 'j.davis@expressl&t.net'),
('Phone', '(503) 421-7800')]),
OrderedDict([('attributes',
OrderedDict([('type', 'Contact'),
('url',
'/services/data/v42.0/sobjects/Contact/0033t0000356R90AAE')])),
('Id', '0033t0000356R90AAE'),
('Name', 'Jane Grey'),
('Title', 'Dean of Administration'),
('Email', 'jane_gray@uoa.edu'),
('Phone', '(520) 773-9050')]),
OrderedDict([('attributes',
OrderedDict([('type', 'Contact'),
('url',
'/services/data/v42.0/sobjects/Contact/0033t0000356R8pAAE')])),
('Id', '0033t0000356R8pAAE'),
('Name', 'Rose Gonzalez'),
('Title', 'SVP, Procurement'),
('Email', 'rose@edge.com'),
('Phone', '(512) 757-6000')]),
OrderedDict([('attributes',
OrderedDict([('type', 'Contact'),
('url',
'/services/data/v42.0/sobjects/Contact/0033t0000356R8qAAE')])),
('Id', '0033t0000356R8qAAE'),
('Name', 'Sean Forbes'),
('Title', 'CFO'),
('Email', 'sean@edge.com'),
('Phone', '(512) 757-6000')]),
OrderedDict([('attributes',
OrderedDict([('type', 'Contact'),
('url',
'/services/data/v42.0/sobjects/Contact/0033t0000356R8rAAE')])),
('Id', '0033t0000356R8rAAE'),
('Name', 'Jack Rogers'),
('Title', 'VP, Facilities'),
('Email', 'jrogers@burlington.com'),
('Phone', '(336) 222-7000')]),
OrderedDict([('attributes',
OrderedDict([('type', 'Contact'),
('url',
'/services/data/v42.0/sobjects/Contact/0033t0000356R8sAAE')])),
('Id', '0033t0000356R8sAAE'),
('Name', 'Pat Stumuller'),
('Title', 'SVP, Administration and Finance'),
('Email', 'pat@pyramid.net'),
('Phone', '(014) 427-4427')]),
OrderedDict([('attributes',
OrderedDict([('type', 'Contact'),
('url',
'/services/data/v42.0/sobjects/Contact/0033t0000356R91AAE')])),
('Id', '0033t0000356R91AAE'),
('Name', 'Arthur Song'),
('Title', 'CEO'),
('Email', 'asong@uog.com'),
('Phone', '(212) 842-5500')]),
OrderedDict([('attributes',
OrderedDict([('type', 'Contact'),
('url',
'/services/data/v42.0/sobjects/Contact/0033t0000356R8tAAE')])),
('Id', '0033t0000356R8tAAE'),
('Name', 'Andy Young'),
('Title', 'SVP, Operations'),
('Email', 'a_young@dickenson.com'),
('Phone', '(785) 241-6200')]),
OrderedDict([('attributes',
OrderedDict([('type', 'Contact'),
('url',
'/services/data/v42.0/sobjects/Contact/0033t0000356R8uAAE')])),
('Id', '0033t0000356R8uAAE'),
('Name', 'Tim Barr'),
('Title', 'SVP, Administration and Finance'),
('Email', 'barr_tim@grandhotels.com'),
('Phone', '(312) 596-1000')]),
OrderedDict([('attributes',
OrderedDict([('type', 'Contact'),
('url',
'/services/data/v42.0/sobjects/Contact/0033t0000356R8vAAE')])),
('Id', '0033t0000356R8vAAE'),
('Name', 'John Bond'),
('Title', 'VP, Facilities'),
('Email', 'bond_john@grandhotels.com'),
('Phone', '(312) 596-1000')]),
OrderedDict([('attributes',
OrderedDict([('type', 'Contact'),
('url',
'/services/data/v42.0/sobjects/Contact/0033t0000356R92AAE')])),
('Id', '0033t0000356R92AAE'),
('Name', 'Ashley James'),
('Title', 'VP, Finance'),
('Email', 'ajames@uog.com'),
('Phone', '+44 191 4956203')]),
OrderedDict([('attributes',
OrderedDict([('type', 'Contact'),
('url',
'/services/data/v42.0/sobjects/Contact/0033t0000356R94AAE')])),
('Id', '0033t0000356R94AAE'),
('Name', "Liz D'Cruz"),
('Title', 'VP, Production'),
('Email', 'ldcruz@uog.com'),
('Phone', '(650) 450-8810')]),
OrderedDict([('attributes',
OrderedDict([('type', 'Contact'),
('url',
'/services/data/v42.0/sobjects/Contact/0033t0000356R95AAE')])),
('Id', '0033t0000356R95AAE'),
('Name', 'Edna Frank'),
('Title', 'VP, Technology'),
('Email', 'efrank@genepoint.com'),
('Phone', '(650) 867-3450')]),
OrderedDict([('attributes',
OrderedDict([('type', 'Contact'),
('url',
'/services/data/v42.0/sobjects/Contact/0033t0000356R96AAE')])),
('Id', '0033t0000356R96AAE'),
('Name', 'Avi Green'),
('Title', 'CFO'),
('Email', 'agreen@uog.com'),
('Phone', '(212) 842-5500')]),
OrderedDict([('attributes',
OrderedDict([('type', 'Contact'),
('url',
'/services/data/v42.0/sobjects/Contact/0033t0000356R97AAE')])),
('Id', '0033t0000356R97AAE'),
('Name', 'Siddartha Nedaerk'),
('Title', None),
('Email', None),
('Phone', None)]),
OrderedDict([('attributes',
OrderedDict([('type', 'Contact'),
('url',
'/services/data/v42.0/sobjects/Contact/0033t0000356R98AAE')])),
('Id', '0033t0000356R98AAE'),
('Name', 'Jake Llorrac'),
('Title', None),
('Email', None),
('Phone', None)]),
OrderedDict([('attributes',
OrderedDict([('type', 'Contact'),
('url',
'/services/data/v42.0/sobjects/Contact/0033t0000356R93AAE')])),
('Id', '0033t0000356R93AAE'),
('Name', 'Tom Ripley'),
('Title', 'Regional General Manager'),
('Email', 'tripley@uog.com'),
('Phone', '(650) 450-8810')])])])