SOSL
SOSL (Salesforce Object Search Language) performs full-text search across multiple objects using the search index.
SOSL (Salesforce Object Search Language) performs full-text search across multiple objects using the search index. Use SOSL when you don't know which object or field contains the data; use SOQL when you do.
Search term; supports * (wildcard), ? (single char), AND, OR, AND NOT, quoted phrases
ALL FIELDS, NAME FIELDS, EMAIL FIELDS, PHONE FIELDS, SIDEBAR FIELDS
Objects and fields to return, each with optional WHERE, ORDER BY, LIMIT
WITH DIVISION, WITH DATA CATEGORY, WITH SNIPPET, WITH NETWORK, WITH USER_MODE Total rows (max 2,000) Dynamic SOSL and binding:
Notes: search terms must be at least two characters; newly inserted records may take a moment to be indexed; in tests, SOSL returns nothing unless you call Test.setFixedSearchResults(ids).
List<List<SObject>> results = [
FIND 'Acme*' IN ALL FIELDS
RETURNING
Account (Id, Name, Industry WHERE Industry != null ORDER BY Name LIMIT 10),
Contact (Id, FirstName, LastName, Email),
Opportunity (Id, Name, StageName)
LIMIT 50
];
List<Account> accounts = (List<Account>) results[0];
List<Contact> contacts = (List<Contact>) results[1];
List<Opportunity> opps = (List<Opportunity>) results[2];String term = 'Lovelace';
List<List<SObject>> r1 = [FIND :term IN NAME FIELDS RETURNING Contact(Id, Name)];
String sosl = 'FIND {' + String.escapeSingleQuotes(term) + '} IN ALL FIELDS RETURNING Lead(Id, Name)';
List<List<SObject>> r2 = Search.query(sosl);