function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
thomastthomast 

Substring searches not possible with SOSL, right?

The last bullet of the SOSL challenge says, "The 'searchContactsAndLeads' method must accept an incoming string as a parameter, find any contact or lead that matches the string as part of either the first or last name and then return those records." 

But the text above says that SOSL searches based on word boundaries, and I couldn't figure out a way to do sub-string searches, ie, one where a search for "mit" would return results for "Smith". I passed the challenge with just a basic SOSL query, but I don't think that it meets that last criterion, strictly speaking. 
pconpcon
SOSL accepts wildcards.  For example
 
FIND '*mit*' IN ALL FIELDS RETURNING Contact, Lead

will find all fields in Contact and Lead that contain "mit"

To do this dynamically you would just do
 
String needle = 'mit';
String queryString = 'FIND \'*' + needle + '*\' IN ALL FIELDS RETURNING Contact, Lead';
List<List<sObject>> results = Search.query(queryString);

For more info

http://www.salesforce.com/us/developer/docs/soql_sosl/Content/sforce_api_calls_sosl_find.htm
http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_dynamic_sosl.htm
thomastthomast

The docs at the first link say "Asterisks match zero or more characters at the middle or end (not the beginning) of your search term." (emphasis added).

And when I run 

FIND {*mit*} IN NAME FIELDS RETURNING Contact(Name, Account.Name), Lead(FirstName, LastName, Company)
in the Dev Console Query Editor, I get no results, when {Smit*} or {Smith} returns the expected rows.