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
Missy LongshoreMissy Longshore 

Trailhead SOSL module of apex class badge

Hi Friends,
I'm finishing up the Apex Basics Badge (yay!) and want to follow the recommended syntax. There's another answer posted on here that passes the challenge but uses syntax that is different than what is taught in the documentation. I really want to understand where I'm going wrong as I think I'm pretty close. Thanks for your feedback!
Here's my code:

public class ContactAndLeadSearch {

    public static void List<List<sObject>> searchContactsAndLeads = [FIND 'Smith' IN NAME FIELDS RETURNING
                                                 Lead (FirstName,LastName),Contact(FirstName,LastName)];
    
    Lead[] searchLeads = (Lead[])searchList[0];
    Contact[] searchContacts = (Contact[])searchList[1];
 
    System.debug('Found the following Lead.');
    for (Lead l : searchLeads) {
        System.debug(l.FirstName);
    }
    System.debug('Found the following contacts.');
    for (Contact c : searchContacts) {
        System.debug(c.LastName + ', ' + c.FirstName);
    }
    
}

Here's the challenge:
Create an Apex class that returns both contacts and leads based on a parameter.
With SOSL you can search against different object types that may have similar data, such as contacts and leads. To pass this challenge, create an Apex class that returns both contacts and leads that have first or last name matching the incoming parameter.
  • The Apex class must be called 'ContactAndLeadSearch' and be in the public scope.
  • The Apex class must have a public static method called 'searchContactsAndLeads'.
  • Because SOSL indexes data for searching, you must create a Contact record and Lead record before checking this challenge. Both records must have the last name 'Smith'. The challenge uses these records for the SOSL search.
  • The return type for 'searchContactsAndLeads' must be 'List<List< SObject>>'
  • The 'searchContactsAndLeads' method must accept an incoming string as a parameter, find any contact or lead that matches the string as part of either the first or last name and then return those records.
My current error message is: unexpected token: 'List' in line 3.
My questions: I'm not sure how to best invoke the public static method and still have the return type 'List..' Thanks for your help!
 
Joaquin_teguiJoaquin_tegui
Hi Missy, 

The error your getting right now is because your puting the word "void" before List. This is because void means that the method has no return value. So when you say public static void List<List<sObject>> searchContactsAndLeads it doesn't understand if its a void method that has no return value or a list method that returns a list.

Also you are not declaring the method in your code, here is how your code should look:
 
public class ContactAndLeadSearch {
    //First you declare the searchContactsAndLeads method with a return type of list,
    
    public static List<List<sObject>> searchContactsAndLeads(){
        
        //Now you declare your List<List<sObject>> variable
        List<List< SObject>> searchlist = [FIND 'Smith' IN NAME FIELDS RETURNING Lead (FirstName,LastName),Contact(FirstName,LastName)];
        
        Lead[] searchLeads = (Lead[])searchList[0];
        Contact[] searchContacts = (Contact[])searchList[1];
        
        
        
        System.debug('Found the following Lead.');
        
        for (Lead l : searchLeads) {
            System.debug(l.FirstName);
        }
        System.debug('Found the following contacts.');
        for (Contact c : searchContacts) {
            System.debug(c.LastName + ', ' + c.FirstName);
        }
        
        //Don't forget to return something
        return searchList;
        
    }
}

Not sure is going to make you pass but i hope i helped!
Missy LongshoreMissy Longshore
Thank you @Joaquln! I followed your suggestion, which made the problems go away, but when I enter the apex code into the execute anonymous window, I got, "Only top-level class methods can be declared static" on Line 3.  Here's the current version of my code which should look similar to what you posted:

public class ContactAndLeadSearch {
//Declare the method searchContactsandLeads with a return type of list
    public static List<List<sObject>> searchContactsAndLeads(){
        //Declare the List<List<sObject>> variable
        List<List<sObject>> searchList = [FIND 'Smith' IN NAME FIELDS RETURNING
                                                 Lead (FirstName,LastName),Contact(FirstName,LastName)];
    
    Lead[] searchLeads = (Lead[])searchList[0];
    Contact[] searchContacts = (Contact[])searchList[1];
 
    System.debug('Found the following Lead.');
    for (Lead l : searchLeads) {
        System.debug(l.FirstName);
    }
    System.debug('Found the following contacts.');
    for (Contact c : searchContacts) {
        System.debug(c.LastName + ', ' + c.FirstName);
    }
        //return something
        return searchList;
}
}

 
Jennifer W. LeeJennifer W. Lee
Hi Missy,

This is what I used to complete the challenge...

public class ContactAndLeadSearch {
 public static List<List<SObject>> searchContactsAndLeads(String name) {
       
     List<List<SObject>> result= [FIND :name IN ALL FIELDS 
                                      RETURNING  Contact(LastName), Lead(LastName)];
        return result;
      
    }
}
Amit Chaudhary 8Amit Chaudhary 8
Please check below post for same issue
1) https://developer.salesforce.com/forums/?id=906F0000000BTk4IAG
2) https://developer.salesforce.com/forums/?id=906F0000000BO5rIAG
 
Public Class ContactAndLeadSearch
{
        Public static List<List<sObject>> searchContactsAndLeads(String searchword)
        {
            String searchQuery = 'FIND \'' + searchword + '\' IN ALL FIELDS RETURNING Lead(Name,FirstName,LastName ), Contact(FirstName,LastName )';
            List<List<sObject>> searchConLead = search.query(searchQuery);
            return searchConLead;
        }
}
NOTE:- if you want to search same keyword in mutliple field then dnt add where you can try IN ALL FIELDS.

Execute below code in In Debug Annonymous window
List<List<sObject>> searchContactLead = ContactAndLeadSearch.searchContactsAndLeads('amit');

List<Lead> leadList = New List<Lead>();
List<Contact> contList = New List<Contact>();

leadList = ((List<Lead>)searchContactLead[0]);
contList = ((List<Contact>)searchContactLead[1]);

for(Lead a:leadList)
{
System.debug('Found following Leads ' + a.Name);
}
for(Contact cts:contList){
System.debug('Found following Contacts ' + cts.FirstName + '' + cts.LastName);
}