Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

When I use SOSL in Apex (search.query();) the return type is List<List<sObject>>, but I've never run across a case in which there is more than one list returned.

For example, this:

string q= 'FIND {"John Doe"} Returning Contact(Id, Email, Name, Account.Name)';
List<List<sObject>> ListOfListOfSObjects = search.query(q);

system.debug('Number of Lists: '+ListOfListOfSObjects.size());
for(Integer i=0; i<ListOfListOfSObjects.size(); i++)
{
    system.debug('List '+(i+1)+' size: '+ListOfListOfSObjects[i].size());
}

Returns:

16:18:42.137 (137179000)|USER_DEBUG|[4]|DEBUG|Number of Lists: 1
16:18:42.137 (137575000)|USER_DEBUG|[7]|DEBUG|List 1 size: 2

When would SOSL return more than one List of sObjects? Did I miss something in the documentation?

share|improve this question

1 Answer 1

up vote 13 down vote accepted

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.

List<List<SObject>> searchList = [FIND 'map*' 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]);
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.