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
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"
Yasemin VuralYasemin Vural
Before createing ContactAndLeadSearch class, I executed code on open execute anonymous window that is below. Because, I thought,  I need to create a contact record and a lead record that they have LastName are Smith. So, SOSL can work to find the Smith on Contact record and Lead record.

Contact c= new Contact();
c.LastName='Smith';
insert c;
Lead l=new Lead();
l.LastName='Smith';
l.Company='ABC';
insert l;

Than I create ContactAndLeadSearch class:

public class ContactAndLeadSearch {
    
    Public static List<List<sObject>> searchContactsAndLeads(String searchword)
    {
        List<List<sObject>> searchList= [FIND :searchword IN ALL FIELDS RETURNING Lead(Name,FirstName,LastName ), Contact(FirstName,LastName )];  
        Lead[] searchLeadss = (Lead[])searchList[0];
        Contact[] searchContacts = (Contact[])searchList[1];
        System.debug('Found the following leads.');
        for (Lead l : searchLeadss) {
            System.debug(l.Name+', '+ l.FirstName+', '+ l.LastName);
        }
        System.debug('Found the following contacts.');
        for (Contact c : searchContacts) {
  
            System.debug(c.LastName + ', ' + c.FirstName);
        }
        system.debug(searchList);
        
        return searchList;
        
    }
    
}


I execute the code  on open execute anonymous window that is below:
ContactAndLeadSearch.searchContactsAndLeads('Smith');

Finally, All things provide me to pass the module. I hope it works for you as well.