Selecting Records with SQLForce

This article explains how to read records from Salesforce using SQLForce.

Simple Record Selection


The selectRecords() method returns data from Salesforce as a list of Python records. This is the most common way to query Salesforce.

For example:

for rec in session.selectRecords(‘SELECT username, FROM User WHERE isActive=True’):

print( “selectRecords(): ” + rec.username + ‘ uses profile ‘ + )

It is important to note that the field names in each Python record will be exactly the same as the names used in the SOQL SELECT statement.

The select() method returns data from Salesforce as a list of lists. Each list has a single value for each column in the SOQL SELECT statement.

For example:

for rec in‘SELECT username, FROM User WHERE lastName LIKE “Smith%”‘):

print( “select(): ” + rec[0] + ‘ uses profile ‘ + rec[1] )

A disadvantage to this approach is how difficult it makes the code to read, especially when querying a large number of columns.

Distinct Record Selection

Unlike SOQL, SQLForce supports ANSI-like “SELECT DISTINCT” statements.

For example,

for rec in session.selectRecords(‘SELECT DISTINCT lastName FROM User’):

print( rec.lastName )

Select UNION

The SQLForce SELECT statement supports ANSI-like “SELECT … UNION SELECT …” statements.

For example,

SELECT lastName FROM Contact WHERE birthDate<>NULL

Select Records From the Recycle Bin

By default, SQLForce will not SELECT records in the Salesforce recycle bin. However, if the QUERY_ALL configuration parameter is set to “true”, all subsequent SELECT statements will include records in the Salesforce recyle bin.

session.setenv(“QUERY_ALL”, “true”)