+ Start a Discussion
Dan BroussardDan Broussard 

SOSL return lists, how do you know what came from what object

One of the challenges was to return names from leads and contacts. 
Code looks like this:
public class ContactAndLeadSearch {
    public static List<List<SObject>> searchContactsAndLeads(String FindName)
    {
        list<list<sObject>> ReturnName = [find :FindName IN ALL FIELDS Returning lead(FirstName, LastName), contact(firstname, lastname)];
        
        return ReturnName;
    }
}
When looking at the return ReturnName, how can you determine from what object the First/Last Name was retrieved? Lead?Contact?
Other examples used the  [0] and [1] in their examples when they were traversing multiple object but they only had one "find" in each object. 
Just curious as I do  understand the [x][y] notation in list<list> concepts.
Best Answer chosen by Dan Broussard
ClintLeeClintLee
In the RETURNING clause you specify which objects you want to search.  In the query above it states RETURNING Account(Name) and Contact(FirstName, LastName, Department).  

SOSL understands how to separate the returned results into two collections.  The first is a list that contains all Accounts matching your search criteria, the second list contains all Contacts matching your search criteria.

All Answers

ClintLeeClintLee
Hi Dan,

Each list of objects that's returned contains only a single object type.  In the query you posted, your method would contain a collection of two lists.  The first list would contain just leads and the second list would contain just contacts.

To dynamically check the type of object that's contained in each list you just need to check the object type of one of the records.

For example.
 
for(List<SObject> objects : searchContactsAndLeads('Dan Broussard'))
{
    SObject obj = objects.get(0);  // get the first object in the list
    
    // use the first object to determine the type of objects in this list.
    System.debug('This list contains ' + obj.getSObjectType());
}

Hope that helps,

Clint
Dan BroussardDan Broussard
I did see the list separated by account and contact in the debug when I ran this code. I do not understand how the code know to separate the two object list generated
List<List<sObject>> searchList = [FIND 'Dan OR Phil OR Wee' IN ALL FIELDS 
                   RETURNING Account(Name),Contact(FirstName,LastName,Department)];

Account[] searchAccounts = (Account[])searchList[0];  //why does this return a list of accounts [0]????
Contact[] searchContacts = (Contact[])searchList[1];  // why does this return a list of contact [1] ????


System.debug('Found the following accounts.');
for (Account a : searchAccounts) {
    System.debug(a.Name);
}

System.debug('Found the following contacts.');
for (Contact c : searchContacts) {
    System.debug(c.LastName + ', ' + c.FirstName);
}
ClintLeeClintLee
In the RETURNING clause you specify which objects you want to search.  In the query above it states RETURNING Account(Name) and Contact(FirstName, LastName, Department).  

SOSL understands how to separate the returned results into two collections.  The first is a list that contains all Accounts matching your search criteria, the second list contains all Contacts matching your search criteria.
This was selected as the best answer
chiranjeevi tuluguchiranjeevi tulugu
it will be returned in the same order as u specify in ur query after RETURNING