• WHO WE ARE
  • WHAT WE DO
    • Salesforce
      • Implementations
        • Sales Cloud
        • Service Cloud
        • CPQ
        • Field Service Lightning
        • Field Service for SMEs
      • Developments
        • Salesforce Customization
        • Custom Application Development
        • AppExchange Product Development
      • Migrations
        • Classic to Lightning Migration
        • Other Systems to Salesforce Migration
      • Integrations
    • Data Science
      • BI Solutions
      • AI/ML solutions
      • Agentic AI
  • HOW WE DO
    • Delivery Model
    • Our Works
  • REACH US
    • Contact Us
    • Careers
  • BLOG
    • WHO WE ARE
    • WHAT WE DO
      • Salesforce
        • Implementations
          • Sales Cloud
          • Service Cloud
          • CPQ
          • Field Service Lightning
          • Field Service for SMEs
        • Developments
          • Salesforce Customization
          • Custom Application Development
          • AppExchange Product Development
        • Migrations
          • Classic to Lightning Migration
          • Other Systems to Salesforce Migration
        • Integrations
      • Data Science
        • BI Solutions
        • AI/ML solutions
        • Agentic AI
    • HOW WE DO
      • Delivery Model
      • Our Works
    • REACH US
      • Contact Us
      • Careers
    • BLOG
  • [email protected]
  • (+91) 44-49521562
Merfantz - Salesforce Solutions for SMEs
Merfantz - Salesforce Solutions for SMEs
  • WHO WE ARE
  • WHAT WE DO
    • Salesforce
      • Implementations
        • Sales Cloud
        • Service Cloud
        • CPQ
        • Field Service Lightning
        • Field Service for SMEs
      • Developments
        • Salesforce Customization
        • Custom Application Development
        • AppExchange Product Development
      • Migrations
        • Classic to Lightning Migration
        • Other Systems to Salesforce Migration
      • Integrations
    • Data Science
      • BI Solutions
      • AI/ML solutions
      • Agentic AI
  • HOW WE DO
    • Delivery Model
    • Our Works
  • REACH US
    • Contact Us
    • Careers
  • BLOG

How to write SOSL Queries in Salesforce

  • November 13, 2018
  • Merfantz Editor
  • Salesforce Developments Tutorial
  • 0

Write SOSL Queries

  • Salesforce Object Search Language (SOSL) is a Salesforce search language that is used to perform text searches in records. Use SOSL to search fields across multiple standard and custom object records in Salesforce. SOSL is similar to Apache Lucene.
  • Adding SOSL queries to Apex is simple—you can embed SOSL queries directly in your Apex code. When SOSL is embedded in Apex, it is referred to as inline SOSL.

Basic SOSL Syntax

       SOSL allows you to specify the following search criteria:

  • Text expression (single word or a phrase) to search for
  • Scope of fields to search
  • List of objects and fields to retrieve
  • Conditions for selecting rows in the source objects

      This is the syntax of a basic SOSL query:

FIND ‘SearchQuery’ [IN SearchGroup] [RETURNING ObjectsAndFields]

  • This is an example of a SOSL query that searches for accounts and contacts that have any fields with the word ‘SFDC’.

List<List<SObject>> searchList = [FIND ‘SFDC’ IN ALL FIELDS RETURNING Account(Name),Contact(FirstName,LastName)];

Differences and Similarities Between SOQL and SOSL

  • Like SOQL, SOSL allows you to search your organization’s records for specific information. Unlike SOQL, which can only query one standard or custom object at a time, a single SOSL query can search all objects.
  • Another difference is that SOSL matches fields based on a word match while SOQL performs an exact match by default (when not using wildcards).
  • For example, searching for ‘Digital’ in SOSL returns records whose field values are ‘Digital’ or ‘The Digital Company’, but SOQL returns only records with field values of ‘Digital’.

SOQL and SOSL are two separate languages with different syntax. Each language has a distinct use case:

  • Use SOQL to retrieve records for a single object.
  • Use SOSL to search fields across multiple objects. SOSL queries can search most text fields on an object.

Prerequisites

Some queries in this unit expect the org to have accounts and contacts. If you haven’t created the sample data in the SOQL unit, create sample data in this unit. Otherwise, you can skip creating the sample data in this section.

  1. In the Developer Console, open the Execute Anonymous window from the Debug menu.
  2. Insert the below snippet in the window and click Execute.

// Add account and related contact
Account acct = new Account(
Name=’SFDC Computing’,
Phone='(415)555-1212′,
NumberOfEmployees=50,
BillingCity=’San Francisco’);
insert acct;
// Once the account is inserted, the sObject will be
// populated with an ID.
// Get this ID.
ID acctID = acct.ID;
// Add a contact to this account.
Contact con = new Contact(
FirstName=’Carol’,
LastName=’Ruiz’,
Phone='(415)555-1212′,
Department=’Wingo’,
AccountId=acctID);
insert con;
// Add account with no contact
Account acct2 = new Account(
Name=’The SFDC Query Man’,
Phone='(310)555-1213′,
NumberOfEmployees=50,
BillingCity=’Los Angeles’,
Description=’Expert in wing technologies.’);
insert acct2;

Using the Query Editor

The Developer Console provides the Query Editor console, which enables you to run SOSL queries and view results. The Query Editor provides a quick way to inspect the database.

It is a good way to test your SOSL queries before adding them to your Apex code. When you use the Query Editor, you need to supply only the SOSL statement without the Apex code that surrounds it.

Let’s try running the following SOSL example:

  1. In the Developer Console, click the Query Editor tab.
  2. Copy and paste the following into the first box under Query Editor, and then click Execute.

FIND {Wingo} IN ALL FIELDS RETURNING Account(Name), Contact(FirstName,LastName,Department)

All account and contact records in your org that satisfy the criteria will display in the Query Results section as rows with fields. The results are grouped in tabs for each object (account or contact).

The SOSL query returns records that have fields whose values match Wingo. Based on our sample data, only the contact has a field with the value Wingo, so this contact is returned.

The search query in the Query Editor and the API must be enclosed within curly brackets ({Wingo}). In contrast, in Apex the search query is enclosed within single quotes (‘Wingo’).

This blog infomation is very useful to learn about SOSL.

We are the ISV Partners and Please reach us for custom development => www.merfantz.com

Author Bio

Merfantz Editor
+ Recent Posts
  • August 4, 2023
    How to Setup CRM Analytics for Financial Services Cloud
  • August 2, 2023
    How to Get Started with Salesforce Hyperforce - Merfantz
  • July 28, 2023
    Health Cloud Mastery: Salesforce Trailhead for Effective Healthcare CRM
  • July 26, 2023
    How to integrate Data cloud and Marketing cloud In Salesforce
  • Previous Use of Schema Builder in Salesforce
  • Next How to Write SOQL Queries in Salesforce
Merfantz Technologies is a leading Salesforce consulting firm dedicated to helping small and medium enterprises transform their operations and achieve their goals through the use of the Salesforce platform. Contact us today to learn more about our services and how we can help your business thrive.

Discover More

Terms and Conditions
Privacy Policy
Cancellation & Refund Policy

Contact Info

  • No 96, 2nd Floor, Greeta Tech Park, VSI Industrial Estate, Perungudi, Chennai 600 096, Tamil Nadu, INDIA
  • (+91) 44-49521562
  • [email protected]
  • 9:30 IST - 18:30 IST

Latest Posts

Comprehensive Overview of Salesforce Agent-Force Platform
Comprehensive Overview of Salesforce Agent-Force Platform June 12, 2025
Salesforce GraphQL Revolutionizing Data Queries in CRM
Salesforce GraphQL: Revolutionizing Data Queries in CRM June 6, 2025
Unlocking the Power of Dynamic Forms in Salesforce Lightning App Builder
Unlocking the Power of Dynamic Forms in Salesforce Lightning App Builder May 29, 2025

Copyright @2023 Merfantz Technologies, All rights reserved