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
MubashirMubashir 

Trailhead challenge SOSL

Hi,
I'm stuck on this trailhead challenge.
Here's the link of the challenge page:

https://developer.salesforce.com/trailhead/force_com_programmatic_beginner/apex_database/apex_database_sosl#challenge
and this is the error message that appears in red:

Challenge not yet complete... here's what's wrong: 
Executing the 'searchContactsAndLeads' method failed. Either the method does not exist, is not static, or does not return the expected search results.


Here is my class:

//The apex class in public domain
public class ContactAndLeadSearch {
    
    //public static method that takes a String parameter
    
    public static SObject[] searchContactsAndLeads(String input) {
        
        //Create list of two lists, one for Contacts and the second for Leads
        List<List<SObject>> searchResults = [FIND :input
                                            IN NAME FIELDS
                                            RETURNING Contact(Name), Lead(Name)];
        
        //Place list of contacts at position 1 and leads at 2
        Contact[] searchContacts = (Contact[])searchResults[0];
        Lead[] searchLeads = (Lead[])searchResults[1];
        
        //return the two lists - I think this is where the problem is.
        return searchContacts;
        return searchLeads;
        
    }

}

After fiddling around a little I have managed to get to the stage where no problems appear in the Problems tab in developer console. But I suspect the code would only execute up to first return method on second last line 'return searchContacts;'

thanks
sandeep sankhlasandeep sankhla
Hi mubashir,

You can simply create a list of sObject and get Lead and Conact in one list and return that list..

P.S. If my answer helps you to solve your problem please mark it as best answer. It will help other to find best answer.

Thanks,
Sandeep
Salesforce Certified Developer 
Phani SurapaneniPhani Surapaneni
Hi Mubashir,

You would need to have return type List<List< SObject>> . Also the last part of questions says - "find any contact or lead that matches the string as part of either the first or last name and then return those records." . You would need to place a criteria for the search on last and first names of contact and lead. I'm thinking following code might workout -

public class ContactAndLeadSearch {
    public static List<List<SObject>> searchContactsAndLeads(String name){
        List<List<sObject>> conLeadLists = [FIND :name IN ALL FIELDS RETURNING Contact(FirstName,LastName WHERE FirstName=:name or LastName=:name),Lead(FirstName,LastName WHERE FirstName=:name or LastName=:name) ];
        Contact[] searchContacts = (Contact[])conLeadLists[0];
        Lead[] searchLeads = (Lead[])conLeadLists[1];
        System.debug('Found the following contacts.');    
        for (Contact c : searchContacts) {
        System.debug(c.LastName + ', ' + c.FirstName);
}
        for (Lead le : searchLeads) {
        System.debug(le.LastName + ', ' + le.FirstName);
}
        return conLeadLists;
    }
}


Thanks,
Phani