REGISTER NOW: Supercharge Your Cloud Integrations and Register for our Webinar on 9/26

Kloudless Blog

Tutorials, case studies and how-tos from our experts

Searching in Salesforce with SOSL Queries

The Salesforce API offers two options for searching for records within Salesforce: SOQL, (Salesforce Object Query Language and SOSL) and SOSL (Salesforce Object Search Language) which are available in Salesforce’s REST API.

How do the two differ? Well, SOQL functions as their version of the SQL SELECT statement for Salesforce Objects (sObjects) and is utilized in their /query endpoint. SOSL on the other hand, has a more flexible syntax and can also be used as a simple keyword search. We’ll be focusing on the latter in this post.

Let’s start by taking a look at the search request:

curl -X GET -H 'Authorization: Bearer [TOKEN]' https://xxx.salesforce.com/services/data/v[XX.X]/search/?q=[search_string]

Pretty straightforward. XX.X is the Salesforce API version, and the q parameter takes search_string as an argument.

So with the search query q=FIND {test} in ALL FIELDS, we might get the results:

As you can see in the search results, the basic information you’d get would only include type and url. You can also see that there are multiple types of sObjects returned in the results. This is one of the advantages of SOSL has over SOQL, as queries will be conducted over multiple object types across the Salesforce database.

However, this advantage can also be a flaw, as a search this wide is bound to be more resource intensive and therefore is limited.

SOSL Search works by filtering records down to the search parameters

It’s possible that the information you’re looking for won’t be returned as the information you’re looking for might not be included as the SOSL query hits a data limit before the filtering even takes place. It’s possible records of certain object types won’t even make it into the search results. In the example above, there are no ContentDocument and Document sObject records at all.

Another inconvenience is the having only id and basic object attributes (type, url) as the return result. It’s a little harder to make sense out of the search results without making additional queries or pulling for extra record metadata.

To remedy this, we can simply be more specific with our search results. If we refine our SOSL query with the following:

Request:

By specifying what field to search in, what sObject types and what additional fields we’d like returned we can not only improve our search accuracy, but also get additional metadata that makes our search results more useful.

Returned data:

At least now we can see what the name or titles of our documents are!

Searching via SOSL is by no means perfect; however, it is still a very useful function that Kloudless supports within our Salesforce integration. I’ve included how to perform searches with SOSL using our Kloudless platform below:

Request:

By setting the lang parameter to SOSL, and the q parameter to the SOSL query, you can query Salesforce with SOSL through the Kloudless CRM search endpoint.

Returned Data:

The lang parameter can also be set to SOQL for a SOQL query, or if left unset, a keyword search as well. For more information on our CRM search, feel free to check out our Kloudless CRM API Docs.

More details on how to specify SOSL syntax can be found in the Salesforce Developer Documentation. You can also test out SOSL queries using the Salesforce Developer Workbench.

References:
https://developer.salesforce.com/docs/atlas.en-us.api_rest.meta/api_rest/resources_search.htm
https://developer.salesforce.com/docs/atlas.en-us.soql_sosl.meta/soql_sosl/sforce_api_calls_sosl_limits.htm

Published By

Kevin Huang