Sub topic 3
Querying data with SOQL and SOSL
This chapter assumes you know SQL-style querying, can read JSON, and already have an access token from the OAuth chapter. The examples use the REST API as the transport, so the same queries also work in Apex and most other Salesforce tools.
Two languages for two jobs
Salesforce has two query languages. SOQL (Salesforce Object Query Language) reads records from one object and its relatives. SOSL (Salesforce Object Search Language) searches text across many objects at once.
| Row | SOQL | SOSL |
|---|---|---|
| Purpose | Retrieve records by field criteria | Find text across objects |
| Scope | One object, plus related objects | Several objects in one request |
| Matching | Exact or pattern filters in `WHERE` | Indexed full-text search |
| REST endpoint | `/query` | `/search` |
SOQL in practice
SOQL looks like SQL but differs in a few ways. There is no SELECT *, so you list every field. There is no JOIN. You traverse relationships by name instead.
- A child-to-parent query uses dot notation:
SELECT LastName, Account.Name FROM Contact. - A parent-to-child query uses a subquery on the plural relationship name:
SELECT Name, (SELECT LastName FROM Contacts) FROM Account. - Aggregates work as expected:
SELECT Industry, COUNT(Id) FROM Account GROUP BY Industry.
Run a query through the REST API like this:
curl -G "https://MyDomain.my.salesforce.com/services/data/v60.0/query" \
--data-urlencode "q=SELECT Id, Name, (SELECT LastName FROM Contacts) FROM Account WHERE Industry = 'Technology' ORDER BY Name LIMIT 5" \
-H "Authorization: Bearer $ACCESS_TOKEN"The response contains totalSize, done, and a records array. A single response returns at most 2,000 records by default. If done is false, the response also has a nextRecordsUrl. Send a GET request to that path to get the next page.
SOSL in practice
SOSL starts with FIND, names the search term in braces, and lists the objects and fields to return.
curl -G "https://MyDomain.my.salesforce.com/services/data/v60.0/search" \
--data-urlencode "q=FIND {Acme} IN NAME FIELDS RETURNING Account(Id, Name), Contact(Id, LastName)" \
-H "Authorization: Bearer $ACCESS_TOKEN"The result is a searchRecords array. Each item has an attributes.type value that tells you which object it came from. Use SOSL for a global search box or when you don't know where a value lives.
Common errors
MALFORMED_QUERY: usually a missing quote around a string value, a trailing comma, or a misspelled keyword.INVALID_FIELD: the field name is wrong, is not visible to the integration user, or you used the relationship name where you needed the field name. Custom fields end in__c.- Slow or failing queries on large objects: a filter that is not selective (one that matches a large share of rows) can time out. Filter on indexed fields such as
Id,Name, or lookup fields. - Unexpectedly missing rows: field-level security and sharing rules apply to the integration user, so the query returns only what that user may see.
When a query would return millions of rows, paging through /query is the wrong tool. Bulk API 2.0, covered later, handles that case.
Exercise
A support tool lets agents type "Acme" into one box and shows matching accounts and contacts. Which language and REST endpoint fit, and what does the tool do if a SOQL result has done: false?
Show answer
Use SOSL through /services/data/v60.0/search, because the agent is searching text across several objects. The done: false case applies only to SOQL: the tool requests the path in nextRecordsUrl to get the next page of records.