trailhead

Choose the Right Search Solution

Learning Objectives

After completing this unit, you’ll be able to:

  • Explain when to create a customized search solution.
  • Describe the difference between SOSL and SOQL.
  • Identify which API protocols are available for search.

Connect to Search with APIs

Keep in mind two main APIs. We’ll talk about two more bonus APIs a little later. Think of it as getting free sides with your main meal.

  • Salesforce Object Query Language (SOQL)
  • Salesforce Object Search Language (SOSL)

Both SOQL and SOSL format text queries in a given API. But that’s where the similarities end. A SOQL query is the equivalent of a SELECT SQL statement and searches the org database. On the other hand, SOSL is a programmatic way of performing a text-based search against the search index. SOSL uses all the super cool, previously mentioned features of the search index.

SOQL and SOSL also have different syntaxes. We’ve included links to developer docs in the resource section. Don’t worry about them now, but we thought you’d like to have them for reference later. You know, in case you want to do a little late-night API reading in your new bunny slippers.

Now that you’ve been properly introduced, here are some guidelines on when to use SOQL or SOSL.

Use SOQL when you know in which objects or fields the data resides and you want to:

  • Retrieve data from a single object or from multiple objects that are related to one another.
  • Count the number of records that meet specified criteria.
  • Sort results as part of the query.
  • Retrieve data from number, date, or checkbox fields.

Use SOSL when you don’t know in which object or field the data resides and you want to:

  • Retrieve data for a specific term that you know exists within a field. Because SOSL can tokenize multiple terms within a field and build a search index from this, SOSL searches are faster and can return more relevant results.
  • Retrieve multiple objects and fields efficiently, and the objects might or might not be related to one another.
  • Retrieve data for a particular division in an organization using the divisions feature, and you want to find it in the most efficient way possible.

Remember when we said that there would be two more API types? Well, we weren’t fibbing. First, meet suggested records API. Perhaps you might already know suggested records by its aliases: auto-suggestion, instant results, type-ahead, or Tommy Two Types. OK, that last one isn’t a real thing.

You’re probably familiar with the behavior, too. Let’s say you’re using a website that sells the coolest running shoes available. They use a customized Salesforce search solution for their knowledge base to encourage a community of runners. You want to know which shoes to buy for trail running (your newest passion, besides this Trailhead module). You start typing trail running in the search bar, and it presents options for articles that include the search terms in the title.

Typeahead example

The search box knew exactly what you wanted to read!

Search Suggested Records and Search Suggested Articles REST methods are available for you to use to bring your own users the same immediate satisfaction. We’ll show you more about how to use those methods later.

We’ve been talking a lot about how to find records inside Salesforce. But what if you have records stored outside of Salesforce that users access for their jobs? Well, we’ve got a solution for that too. Introducing Salesforce Federated Search. It’s the new way for users to search for items stored outside of Salesforce—and this is the best part—all while remaining inside Salesforce Classic, Salesforce Console, or Lightning Experience. Talk about a cherry on top.

Salesforce Federated Search allows you to make the global search box an external search engine. When Federated Search is set up, we transfer the user’s query to the external engine, which searches the external sources. The results are returned right in the Salesforce search results. We do this through the Salesforce Federated Search connector. The connector is built using the OpenSearch specification, so you can plug in any search engine that conforms to this industry standard.

It’s important to know that Salesforce Federated Search doesn’t go through the Salesforce search index, meaning that all those cool Salesforce advanced features aren’t applied. Instead, results are returned according to the external search provider, which is cool too.

Next, we’re going to talk about how to use protocols to send SOSL and SOQL queries. No rest for the search enthusiast!

Send Queries with Protocols

You can write all the beautiful search queries that you want. But they aren’t any good unless you use an API protocol like REST, SOAP, or Apex to actually send them. It’s like writing a love letter and leaving it on your desk. Sigh. Such a waste.

Just like any group of friends, some protocols get along better with some APIs than others. In general, we talk about queries with SOQL and searches with SOSL.

  • Query (REST) and query() (SOAP)—Executes a SOQL query against the specified object and returns data that matches the specified criteria.
  • Search (REST) and search() (SOAP)—Executes a SOSL text string search against your org’s data.

More resources to perform other common search tasks, like auto-suggesting records, articles, and queries, are also available. And, if you’d rather not use SOSL or SOQL, consider Parameterized Search in REST. Instead of a search string in the URL, you use parameters (hence the name) in the URL.

As for Apex, you can use SOQL or SOSL on the fly by surrounding the statement in square brackets. You can also use a Search Class to perform dynamic SOSL queries and a Search Namespace for getting search results and suggestion results.

We’ve included a handy list in the resources section of this unit. And we’re going to give you an introduction to using the protocols in the next unit to get you started. Be sure to read up on the developer docs for all the information and examples. Just not right now. You’ve got a quiz to take!

retargeting