SalesforceBlue

Feel the rhythm of Salesforce

Apex

Apex SOSL Simplified

Apex SOSL(Salesforce Object Search Language) lets you search in the database without doing an Apex SOQL. It is a free text search.

The key advantage of SOSL is that it can search multiple objects and fields in a single query. It can search across all the searchable fields except dates, numbers & Ids.

SOSL can return a maximum of 2000 records at a time. If the search is on multiple objects then the total maximum search results combined for all the objects will be 2000 records.

So when we have a SOQL that can also do a search for you by querying in the objects and in the respective fields then what is the need for a SOSL?

The answer is in a synchronous apex transaction you can only have 100 SOQL queries. Suppose you wanted to check in 2 objects such as Lead & Contact that if a given value exists in the record for those objects then you have to write 2 SOQL queries.

If the object count increases from 2 to 3 and so on you may end up with a large number of SOQL queries. This can be avoided and reduced to a signal query by using SOSL.

For Example, Let’s say we are creating a utility that confirms that a given email value is present in Contact & Lead records. We can create this using Apex SOSL instead of writing 2 SOQL queries.

public class BlueUtility {
    public Boolean isLeadAndContactPresent(String email) {
        List<List<SObject>> search_results = [FIND :email IN ALL FIELDS RETURNING Contact (Id, Name), Lead(Id, Name)];

        List<Contact> conList = (List<Contact>)search_results[0];
        List<Lead> leadList = (List<Lead>)search_results[1];

        if(!conList.isEmpty() && !leadList.isEmpty()) {
            return true;
        } else {
            return false;
        }
    }
}

Let’s split the above code block into finer segments to see what is happening.

List<List<SObject>> search_results = [FIND :email IN ALL FIELDS RETURNING Contact (Id, Name), Lead(Id, Name)];

In the above code block, we are writing the SOSL query using the FIND keyword.

ALL FIELDS refers that search the given parameter in all the searchable fields.

Contact(Id, Name) & Lead(Id, Name) refers that if the records are found then return a list of those records with Id & Name fields. If you don’t specify any values then Id will come by default.

List<List<SObject>> search_results = [FIND :email IN ALL FIELDS RETURNING Contact (Id, Name), Lead(Id, Name)];
List<Contact> conList = (List<Contact>)search_results[0];
List<Lead> leadList = (List<Lead>)search_results[1];

SOSL returns a list containing a list of SObject that is List<List<Sobject>>

We are iterating the return list and casting it back to the concrete or actual object that is List<Contact> & List<Lead>

The return list has the same sequence of respective records of Object as we have specified in the SOSL query. Thus, search_results[0] has Contact and search_results[1] has to Lead.

Thank you for visiting SalesforceBlue.com
If you have any queries feel free to write down a comment below 🙂


One thought on “Apex SOSL Simplified

  • Thanks for your blog, nice to read. Do not stop.

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *