What is the basic difference between SOQL vs SOSL?

235    Asked by DorineHankey in Salesforce , Asked on Jun 20, 2024

I am currently engaged in a particular task that is related to working on a salesforce project for a particular client who wants to search for a specific record across multiple objects quickly. They are concerned about the performance and also the accuracy of the search. How can I decide whether to use the SOQL or SOSL for this task? 

Answered by Miura Baba

In the context of Salesforce, here are the differences between both are given:-

Search optimization

If the client is searching the requirements which involve searching specific fields on one or more Objects, the SOQL is the preferred choice.

Search across multiple Object

If the search needs to span multiple objects and the client is looking for w keyword-based search rather than a field-specific search then the SOSL is more suitable.

Performance considerations

For large datasets or even complex search criteria, the performance becomes critical. SOQL tends to be more effective for targeted searches on specific fields, whereas SOSL can be faster for broader keyword-based searches.

Here is an example given below of using the SOQL in a Salesforce apex class for performing various operations like querying, filtering, and sorting records:-

Public class SOQLExampleController {
    // Method to fetch a list of accounts sorted by Name
    Public static List getSortedAccounts() {
        List sortedAccounts = new List();
        // Query to fetch accounts sorted by Name in ascending order
        sortedAccounts = [SELECT Id, Name, Industry, AnnualRevenue
                          FROM Account
                          ORDER BY Name ASC];
        Return sortedAccounts;
    }
    // Method to fetch accounts based on a specific industry
    Public static List getAccountsByIndustry(String industryName) {
        List filteredAccounts = new List();
        // Query to fetch accounts belonging to a specific industry
        filteredAccounts = [SELECT Id, Name, Industry, AnnualRevenue
                            FROM Account
                            WHERE Industry = :industryName];
        Return filteredAccounts;
    }
    // Method to fetch accounts with annual revenue greater than a specified amount
    Public static List getAccountsByRevenue(Double minRevenue) {
        List highRevenueAccounts = new List();
        // Query to fetch accounts with annual revenue greater than the specified amount
        highRevenueAccounts = [SELECT Id, Name, Industry, AnnualRevenue
                               FROM Account
                               WHERE AnnualRevenue > :minRevenue];
        Return highRevenueAccounts;
    }
    // Method to fetch accounts along with their related contacts
    Public static List getAccountsWithContacts() {
        List accountsWithContacts = new List();
        // Query to fetch accounts along with their related contacts
        accountsWithContacts = [SELECT Id, Name, (SELECT Id, FirstName, LastName FROM Contacts)
                                FROM Account];
        Return accountsWithContacts;
    }
}
Here is an example given below of using SOSL in a Salesforce apex class for performing keyword based searches across multiple object:-
Public class SOSLExampleController {
    // Method to search for records using SOSL
    Public static List> searchRecords(String keyword) {
        List> searchResults = new List>();
        // SOSL query to search for the keyword in multiple objects
        searchResults = [FIND :keyword
                         IN ALL FIELDS
                         RETURNING Account(Id, Name), Contact(Id, FirstName, LastName), Opportunity(Id, Name, StageName)];
        Return searchResults;
    }
    // Method to search for records in specific fields using SOSL
    Public static List> searchInFields(String keyword, List fieldsToSearch) {
        List> searchResults = new List>();
        // Construct SOSL query dynamically based on fields to search
        String soslQuery = ‘FIND :keyword IN ‘;
        For(String field : fieldsToSearch) {
            soslQuery += field + ‘, ‘;
        }
        soslQuery += ‘RETURNING Account(Id, Name), Contact(Id, FirstName, LastName), Opportunity(Id, Name, StageName)’;
        // Execute the dynamically constructed SOSL query
        searchResults = [FIND oslQuery USING KEYWORDS :keyword];
        return searchResults;
    }
}

Your Answer

Answer (1)

This is a really good post! I sincerely appreciate it and am forward to read more of your writings in the future. mapquest

1 Month

Interviews

Parent Categories