You need to sign in to do that
Don't have an account?
Dan 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.
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.
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
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.
Hope that helps,
Clint
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);
}
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.
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);
}