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
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
JaimeBidJaimeBid
Hi Dan, regarding your question

See that first you create a list of list, where Account would be list # 0 and Contact the list #1:

List<List<sObject>> searchList = [FIND 'Dan OR Phil OR Wee' IN ALL FIELDS 
                   RETURNING Account(Name),Contact(FirstName,LastName,Department)];

So, then you "extract" the data from the searchlist in 0 position, and place it inside the searchAccounts  variable:

Account[] searchAccounts = (Account[])searchList[0];  

And the same with contact...

Hope this helps... someone... even 5 years after 😊

This would be another way to write it, with same result:

List<List<sObject>> searchList = [FIND 'Wingo OR SFDC' IN ALL FIELDS 
                                  RETURNING Account(Name),Contact(FirstName,LastName,Department)];

List <Account> searchAccounts = searchList.get(0);

List <Contact> searchContacts = searchList.get(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);
}