Skip to content
Software capabilities

Salesforce capability

How to query Salesforce records with SOQL

Send a SOQL query to the Salesforce REST Query resource for an integration. Use sf data query to run the same query from a terminal against an authorized org.

Documentation reviewed

Ways to do it

API

GET /services/data/v{version}/query

URL-encode the SOQL statement in q. If the first response has more records, fetch the URL it returns in nextRecordsUrl.

Requirements
The Salesforce org’s instance URL, an authorized OAuth token and access to the target objects and fields.
Limitations
The query response is paginated. Follow nextRecordsUrl until done is true.

Example

GET /services/data/vXX.X/query?q=SELECT%20Id%2C%20Name%20FROM%20Account%20LIMIT%205
Read source: Salesforce REST Query resource

CLI

sf data query

Run a SOQL statement with --query or pass a query file. Use --target-org to select the authenticated org.

Requirements
Salesforce CLI and an authenticated org alias or username with access to the queried data.
Limitations
Salesforce recommends sf data export bulk when a query returns more than 10,000 records.

Example

sf data query --target-org EXAMPLE_ORG --query "SELECT Id, Name FROM Account LIMIT 5"
Read source: Salesforce CLI data query reference