+ Start a Discussion
Sejal PandeSejal Pande 

"Challenge Not yet complete... here's what's wrong: The Lead and Contact records with the last name 'Smith' were not found. Please add these records for this challenge." Lead and Contact record already created still getting error message

Challenge Not yet complete... here's what's wrong: 
The Lead and Contact records with the last name 'Smith' were not found. Please add these records for this challenge.

I have created a Lead and Contact Record with last name 'Smith' , but I am still getting this error message on trailhead.

Below is my code:
public class ContactAndLeadSearch {

   
    public static List<List<SObject>> searchContactsAndLeads(String str)
    {
        List<List<sObject>> searchContactsAndLeadsList = [FIND :str IN Name FIELDS RETURNING Contact(FirstName,LastName) ,Lead(FirstName,Lastname)];
        system.debug(searchContactsAndLeadsList);
        
        

        return searchContactsAndLeadsList;
        
    }


}

Kindly assist.
Regards,
​Sejal 
Arvind KumarArvind Kumar
Hi Sejal,

Use below class.
 
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;
        }
}

It would be helpful for you.

Thanks,
Arvind Kumar
Amit Chaudhary 8Amit Chaudhary 8
First Create one lead and one contact record with last Name Smith .

In your code you are using In Name but you should use IN All Field like below
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;
        }
}

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

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);
}

Please check below post
https://developer.salesforce.com/forums/?id=906F0000000BO5rIAG
https://developer.salesforce.com/forums/?id=906F0000000BTk4IAG

Please let us know if this will help you
 
Sejal PandeSejal Pande
I tried doing it this way. But it still shows the same error. While I execute in the Anonymous window it displays the right output but when I check the challenge on Tailhead it shows an error message. 
Amit Chaudhary 8Amit Chaudhary 8
If you want i can look into your org. Please connect to me on my email id "amit.salesforce21@gmail.com"