Sub topic 14
Running SOSL through the REST API
This chapter assumes you can write a SOSL query with FIND and RETURNING, and that you know what an OAuth access token is. You already ran SOSL from Apex. The REST API lets code outside Salesforce, such as a web app or a script, send the same query over HTTP.
The search endpoint
The REST API exposes SOSL at the search resource. You send a GET request with the query in the q parameter:
GET /services/data/v62.0/search/?q=<URL-encoded SOSL>Two things differ from Apex:
- The query is plain text in a URL, so it must be URL-encoded. Spaces, braces, and parentheses all need encoding.
- There are no bind variables. Your client builds the full string before sending it.
Sending a query with curl
The -G flag with --data-urlencode lets curl do the encoding for you. This example uses the braces form of the search expression:
curl -G "https://yourInstance.my.salesforce.com/services/data/v62.0/search/" \
-H "Authorization: Bearer $ACCESS_TOKEN" \
--data-urlencode 'q=FIND {Acme*} IN NAME FIELDS RETURNING Account(Id, Name LIMIT 5), Contact(Id, LastName)'Set ACCESS_TOKEN to a valid OAuth token first. Replace yourInstance with your org's My Domain host name.
Reading the response
Apex gives you one list per object in RETURNING. REST gives you a single flat list called searchRecords. Each record carries an attributes block that names its object type:
{
"searchRecords": [
{
"attributes": {
"type": "Account",
"url": "/services/data/v62.0/sobjects/Account/001000000000001AAA"
},
"Id": "001000000000001AAA",
"Name": "Acme Corp"
},
{
"attributes": {
"type": "Contact",
"url": "/services/data/v62.0/sobjects/Contact/003000000000001AAA"
},
"Id": "003000000000001AAA",
"LastName": "Acme"
}
]
}Because the list is flat, your client must read attributes.type to tell an Account from a Contact. Do not rely on position, as you would with results[0] in Apex.
Escaping and security
The escaping rules from dynamic SOSL still apply. The search term is parsed by Salesforce, so user input can change the meaning of the query. Two layers are involved:
- Escape SOSL reserved characters in the term, such as
?,&,|,!,{,},[,],(,),^,~,*,:,\,",',+, and-. Put a backslash before each one. - URL-encode the whole query after that. Use your HTTP library's encoder, not manual string edits.
The query runs as the user who owns the access token. Sharing rules and field-level security for that user decide which records and fields come back. This differs from Apex, where with sharing and WITH USER_MODE control the behavior.
Limits to remember
- A SOSL query returns at most 2,000 rows, whichever interface you use.
- Each request counts against your org's daily REST API request limit.
- A very long query can exceed the URL length limit. If the encoded
qvalue gets that long, simplify the query.
Exercise
A client calls the search endpoint with RETURNING Account(Id, Name), Contact(Id, LastName). How does it find out which returned records are Contacts, and why can it not use the list position as in Apex?
Show answer
It reads attributes.type on each item in searchRecords. REST returns one flat list that mixes all the objects, so there is no results[0] and results[1] split by object.