+ Start a Discussion
Jason FungJason Fung 

Why SOSL search query variable (searchList) has a variable type of a List within a List?

Going through the Trailhead tutorial on SOSL search query and is wondering why the searchList variable is a List within a List. Can't we just use <List<sObject> searchList instead of List<List<sObject>> searchList ? Anyone here can kindly explain?

List<List<sObject>> searchList = [FIND 'Wingo OR SFDC' IN ALL FIELDS RETURNING Account(Name),Contact(FirstName,LastName,Department)];
 
Best Answer chosen by Jason Fung
Ishwar ShindeIshwar Shinde
SOSL has the ability to search across multiple objects in one query. So if you do something like the following it will return results from Accounts, Contacts, Opportunities, and Leads. The order of sobject is depends the sequence you have used in sosl query. List<sObject> can hold all object data, but it will lot more easier with List<sObject> as we already know the which sObject it is referring to..
 
List<List<SObject>> searchList = [FIND 'test*' IN ALL FIELDS RETURNING Account (Id, Name), Contact, Opportunity, Lead]; 

Account [] accounts = ((List<Account>)searchList[0]); Contact [] contacts = ((List<Contact>)searchList[1]); Opportunity [] opportunities = ((List<Opportunity>)searchList[2]); 
Lead [] leads = ((List<Lead>)searchList[3]);

Hope it willl help !!!