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:

  • Email
  • 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 Email Phone
0 0033t0000356R8wAAE Stella Pavlova SVP, Production [email protected] (212) 842-5500
1 0033t0000356R8xAAE Lauren Boyle SVP, Technology [email protected] (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 [email protected] (520) 773-9050
5 0033t0000356R8pAAE Rose Gonzalez SVP, Procurement [email protected] (512) 757-6000
6 0033t0000356R8qAAE Sean Forbes CFO [email protected] (512) 757-6000
7 0033t0000356R8rAAE Jack Rogers VP, Facilities [email protected] (336) 222-7000
8 0033t0000356R8sAAE Pat Stumuller SVP, Administration and Finance [email protected] (014) 427-4427
9 0033t0000356R91AAE Arthur Song CEO [email protected] (212) 842-5500
10 0033t0000356R8tAAE Andy Young SVP, Operations [email protected] (785) 241-6200
11 0033t0000356R8uAAE Tim Barr SVP, Administration and Finance [email protected] (312) 596-1000
12 0033t0000356R8vAAE John Bond VP, Facilities [email protected] (312) 596-1000
13 0033t0000356R92AAE Ashley James VP, Finance [email protected] +44 191 4956203
14 0033t0000356R94AAE Liz D'Cruz VP, Production [email protected] (650) 450-8810
15 0033t0000356R95AAE Edna Frank VP, Technology [email protected] (650) 867-3450
16 0033t0000356R96AAE Avi Green CFO [email protected] (212) 842-5500
17 0033t0000356R97AAE Siddartha Nedaerk None None None
18 0033t0000356R98AAE Jake Llorrac None None None
19 0033t0000356R93AAE Tom Ripley Regional General Manager [email protected] (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', '[email protected]'),
                            ('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', '[email protected]'),
                            ('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', '[email protected]'),
                            ('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', '[email protected]'),
                            ('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', '[email protected]'),
                            ('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', '[email protected]'),
                            ('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', '[email protected]'),
                            ('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', '[email protected]'),
                            ('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', '[email protected]'),
                            ('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', '[email protected]'),
                            ('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', '[email protected]'),
                            ('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', '[email protected]'),
                            ('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', '[email protected]'),
                            ('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', '[email protected]'),
                            ('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', '[email protected]'),
                            ('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', '[email protected]'),
                            ('Phone', '(650) 450-8810')])])])