Sub topic 10
Query records with SOQL
This chapter assumes you have installed the sf CLI, authorized an org, and set a default target org. It also assumes you know basic Salesforce concepts such as objects and fields. If sf org display shows your default org, you are ready.
What the command does
SOQL (Salesforce Object Query Language) is the query language for Salesforce records. It looks like SQL, but you query objects and fields instead of tables and columns. The CLI sends your query to an org with sf data query and prints the results.
sf data query --query "SELECT Id, Name FROM Account LIMIT 5"The command uses your default target org. It prints the matching records as a table, with one column for each field you selected.
How the request flows
The CLI reads the default org from configuration, sends the query through the org's API, and formats the response.
Choose the org
To query a different org, add --target-org (short form -o) with an alias or username. This overrides the default for that one command.
sf data query --query "SELECT Id, Name FROM Account LIMIT 5" --target-org myorgUse this form in scripts. It makes each command say which org it uses, so a changed default cannot send a query to the wrong place.
Write useful queries
A few clauses cover most daily work.
| Clause | Purpose | Example |
|---|---|---|
| `WHERE` | Filter records | `WHERE Industry = 'Energy'` |
| `ORDER BY` | Sort results | `ORDER BY CreatedDate DESC` |
| `LIMIT` | Cap the number of rows | `LIMIT 10` |
| Relationship field | Read a parent's field | `Account.Name` on a Contact |
This query combines them:
sf data query --query "SELECT Id, Name, Industry FROM Account WHERE Industry = 'Energy' ORDER BY Name LIMIT 10"String values in SOQL use single quotes. Wrap the whole query in double quotes so the shell passes it as one argument.
Quoting and shell pitfalls
Most first-time errors come from quoting. Follow these rules:
- Use double quotes around the query and single quotes around SOQL string values, as in the examples above.
- In a Unix shell, a
$inside double quotes is treated as a variable. Escape it or use a different quoting style if your query contains one. - Keep a long query on one line, or use shell line continuations carefully.
- Field and object API names are required. A custom field ends in
__c, for exampleRegion__c.
Common errors
| Message or symptom | Likely cause | Fix |
|---|---|---|
| `No default target org` or an auth error | No default org is set, or the login expired | Run `sf config set target-org myorg`, or log in again |
| `INVALID_FIELD` | Misspelled field, or a field you cannot access | Check the API name and your field-level security |
| `MALFORMED_QUERY` | Syntax error, such as a missing comma | Compare the query with the clause order: `SELECT`, `FROM`, `WHERE`, `ORDER BY`, `LIMIT` |
| `sObject type 'X' is not supported` | Wrong object name | Use the API name of the object |
SOQL clauses must appear in that order. Writing LIMIT before ORDER BY is a syntax error.
Query more than the default API
By default, sf data query uses the standard API. Some objects, such as metadata records exposed through the Tooling API, need the --use-tooling-api flag. You can also add --result-format to choose human, CSV, or JSON output. The JSON option is covered in a later chapter.
sf data query --query "SELECT Id, Name FROM ApexClass LIMIT 5" --use-tooling-apiTo see every flag the command accepts, run sf data query --help.
Exercises
Exercise
You run sf data query --query "SELECT Id, Name FROM Contact LIMIT 3" and the CLI reports that no target org is set. Name two ways to fix this.
Show answer
Set a default with sf config set target-org myorg, or add --target-org myorg (or -o myorg) to the command. Logging in with sf org login web --alias myorg --set-default also sets the default.
Exercise
Write a command that returns the ten most recently created accounts from the org with the alias myorg, showing Id and Name.
Show answer
sf data query --query "SELECT Id, Name FROM Account ORDER BY CreatedDate DESC LIMIT 10" --target-org myorg