Dynamic SOSL: Salesforce Object Search Language (SOSL) | String Literal Parsing Issue in Apex with SOSL

by - 10:43 PM

Recently, I came across a situation where I am supposed to pull data through SOSL query, though SOSL is used relatively rare than SOQL, but I found myself stucked while firing a dynamic SOSL query in parsing the string 

Before we move on, I would like to quickly share how SOSL works and how to make it dynamic 

When to use SOSL Query : 

SOSL is primarily used in a situation when a developer needs to query a field on all Salesforce standard objects , for example if I want to query the database for the field titled  'Name' in my org in Salesforce, I prefer to chose SOSL query as SOQL only allows to query one object at time.

Clearly the SOSL run for all standard objects and return the data as list for every objects it goes through and henceforth the data in return would have more than one list, therefore SOSL always return List<List<SObjects>> 

How to write SOSL Query ?

Here is pretty simple example 

In the code below, I am trying to return a list of accounts, contacts, opportunities, and leads that begin with the phrase map:
List<List<SObject>> searchList = [FIND 'Name*' IN ALL FIELDS RETURNING Account (Id, Name), Contact, Opportunity, Lead];

Now for each object, the SOSL query will run consecutively and in same order as mentioned, to retrieve each list, you can pull the data using indexing show below 

Account [] accounts = ((List<Account>)searchList[0]);
Contact [] contacts = ((List<Contact>)searchList[1])

Clearly each listed can be fetched through indexing, now you have to make sure you are not passing the SOSL through Trigger, the SOSL cannot be fired through Trigger

How to write Dynamic SOSL ? 

SOSL become dynamic when you pass on the 'field' dynamic and for  this  you have to create dynamic query string, don't be panic this can achieved in very simple way here

lets create a string named queryString and pass on the value of field (to be searched dynamically)

String QueryString =  'FIND\'Edge*\'IN ALL FIELDS RETURNING Account(id,name),Lead';
List<List <sObject>> Query = search.query(queryString);

Some Tweak to Notice 

Apex compiler throw an error when you try to pass on string like show below in example

String queryString = 'Find' + ' '*Name' ' + IN ALL FIELDS RETURNING Account(id,name),Lead';

List<List <sObject>> Query = search.query(queryString);

Remember Apex compiler do not support doubleQuotes (" ") not nested single quotes ( ' ' '  ') hence neither  ( " '*Name' ")  nor ( ' '*Name ' ') will work

the only fix is using \ (backslash here) , if you want to try to understand clearly, just trying this code  in Apex Console editor

So when you type : 

string s ='\';
 this cannot be parsed as you have escaped the closing single quote, so the expectation is that there will be another one.

Now when you type:

string s2 ='\\';

This is then allowed to be parsed, here is another example 

string s ='Austin is world's best place to live';

This will through complier error, now here is the simple fix 

string s ='Austin is world\'s best place to live';

Now if you do system.Debug(s) output would be -  Austin is world's best place to live';

I believe this issue will help you in learning SOSL query and taking care of this string parse issue in Dynamic SOSL

You May Also Like