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
Rishabh AgrawalRishabh Agrawal 

Trailhead challenge : create an Apex class that returns both contacts and leads that have first or last name matching the incoming parameter.

public class ContactAndLeadSearch {
public static List<List<SObject>> searchContactsAndLeads(string FN)
{
    List<List<sObject>> searchList = [FIND 'FN' IN all fields 
RETURNING Contact(FirstName,LastName) ,Lead(FirstName,Lastname)];
//Contact[] searchContacts = (Contact[])searchList[0];
//Lead[] searchLeads = (Lead[])searchList[1];
return searchList;
   

}
}

Please someone correct this code ,, this one was nt fulfilling the challenge req.
Arunkumar RArunkumar R
Hi Rishabh,

Hope you are trying the below trailhead contest,
https://developer.salesforce.com/trailhead/force_com_dev_beginner/apex_database/apex_database_sosl?state=id

Make sure the following inorder to complete your challenge,

1. You must create a Contact record and Lead record before checking this challenge. Both records must have the last name 'Smith'. 
 
public class ContactAndLeadSearch
{
    public static List<List<SObject>> searchContactsAndLeads(String str)
    {
        List<List<sObject>> searchList = [FIND :str IN ALL FIELDS RETURNING Contact(FirstName,LastName) ,Lead(FirstName,Lastname)];
        return searchList;
    }

}
Rishabh AgrawalRishabh Agrawal
I already created records but still it showing error . may be some problem with method definition.
Arunkumar RArunkumar R
You can delete your existing class and paste the above class that i provided. Try it again.
Satyanarayana PusuluriSatyanarayana Pusuluri
Hi,

Below code is working fine.
Before go to the challenge create contact and lead with last name as "Smith"

public class ContactAndLeadSearch
{
    public static List<List<SObject>> searchContactsAndLeads(String str)
    {
        List<List<sObject>> searchContactsAndLeadsList = [FIND :str IN ALL FIELDS RETURNING Contact(FirstName,LastName) ,Lead(FirstName,Lastname)];
        return searchContactsAndLeadsList ;
    }

}

Thanks & Regards,
Satya P
Krishna YalavarthiKrishna Yalavarthi
Hai 
Arunkumar R

In This Below code is Not working please to be slove in This error
The error to be show in
 "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."


public class ContactAndLeadSearch

 {

    public static List<List<SObject>>searchContactsAndLeads(String str)

     {

        List<List<sObject>> searchList = [FIND :str IN ALL FIELDS RETURNING Contact(FirstName,LastName) ,Lead(FirstName,Lastname)];

        return searchList;

     }

 }
Vinod sanneboinaVinod sanneboina
hi
To solve this challenge firstly u have to create an record in Contact and Lead.But when creating records we can give first name any thing but last name should be Smith in both records. This Answer will solve the challenge
 
Nirmallya_GhoshNirmallya_Ghosh
Try The Following code - 
public static List<List<sObject>> searchContactsAndLeads(String lastName) {
        List<List<sObject>> searchList = [FIND :lastName IN NAME FIELDS 
                   RETURNING Contact(Name),Lead(FirstName)];
        return searchList;
}

Thanks 
Nirmallya Ghosh
samyuktha reddy etikayalasamyuktha reddy etikayala
Hi Dear,

public class ContactAndLeadSearch
{
    public static List<List<SObject>> searchContactsAndLeads(String str)
    {
        List<List<sObject>> searchContactsAndLeadsList = [FIND :str IN ALL FIELDS RETURNING Contact(FirstName,LastName) ,Lead(FirstName,Lastname)];
        return searchContactsAndLeadsList ;
    }

}

After writing this code, create the last name as smith in lead and contact, then this challenge will get success.
Verma, ManishVerma, Manish
Hi,

You can use the code below:
 
public class ContactAndLeadSearch {
    public static List<List<SObject>> searchContactsAndLeads (String param){
        
        List<List<SObject>> searchedResults = [FIND :param IN NAME FIELDS RETURNING
                                              Contact(FirstName, LastName),
                                              Lead(Name, LastName, Company)];
        
        return searchedResults;
    }
}

This is working like magic for me. Try it.

Hope this helps!!
Muhammad AlaaMuhammad Alaa
Create the Contact and Lead both with Last Name = Smith [Hint: Use the Quick Create on the Left Side of the Screen]
Then create the class as follows:
public class ContactAndLeadSearch {
    public static List<List<SObject>> searchContactsAndLeads(String sSearchParameter){
        List<List<SObject>> lstContactsAndLeads = [FIND :sSearchParameter IN NAME FIELDS RETURNING
                                                  Contact(LastName),
                                                  Lead(LastName)];
        return lstContactsAndLeads;
    }
}

 
Ravi Havashetty 8Ravi Havashetty 8
The below code is working fine. 
Public class ContactAndLeadSearch{

    Public static list<list<sobject>> searchContactsAndLeads(String strName){
    
        List<List<sobject>> searchList =[FIND :strName IN ALL FIELDS RETURNING Contact(FirstName,LastName), Lead(FirstName,LastName)];
        
        return searchList;
    }
}
Pedro KalvaPedro Kalva
The secret is ":" for indicate a variable!!!
Caleb KuesterCaleb Kuester
@Pedro: See this is why I Googled the answer before even trying. Oddball little details like this that are not anywhere in the tutorial will make the process of learning frustrating. 
Chris Talbot 15Chris Talbot 15
Hi,

New to this but very frustrated, I have created Contacts and Leads and pretty sure the code is right, it just keeps failing saying that the Contacts and Leads are not there!

public class ContactAndLeadSearch 
{
    public static List<List<SObject>> searchContactsAndLeads(String SearchValue)
    {
        List<List<SObject>> ContactsandLeadsList = 
            [FIND :SearchValue IN ALL FIELDS
            RETURNING 
            Contact(FirstName,LastName),
            Lead(Name,LastName,Company)];
        return ContactsandLeadsList;
      }
}

The Lead and Contact records with the last name 'Smith' were not found. Please add these records for this challenge.  Had similar problems with other challenges, any thoughts?
Chris Talbot 15Chris Talbot 15
Issue found, when I logged into my Developer account instead of Trailhead and ran the same thing it worked.  VERY frustrating and wasted a long time pulling my hair out thinking it was me!
alison neeralison neer
Just a note for creating a lead, Company is a required field. 
Lead lead = new Lead(
   FirstName = 'John',
   LastName = 'Smith'
   Company = 'Company Name'
);
Rajat Sharma 31Rajat Sharma 31

First create a contact and a lead record with last name as smith .

 

public class ContactAndLeadSearch {
public static list<list<sobject>> searchContactsAndLeads(string lname)
{
    list<list<sobject>> LS = [FIND :lname in Name Fields returning Lead(lastname,firstname where
                                                                         lastname =: lname or firstname =: lname
                                                                         ),
                                                                       contact(lastname,firstname where
                                                                       lastname =: lname or firstname =: lname)];
    system.debug('Lead-->'+LS[0]);
    system.debug('Contact-->'+LS[1]);
    return LS;
}
}


IN Open Execute Anonymous Window :

ContactAndLeadSearch.searchContactsAndLeads('smith');

Mukesh pal 7Mukesh pal 7
ITS HELPFULL FOR ME
 
Isha AmoliIsha Amoli
Hi All,
Try the below code, this works for me to complete the Trailhead challenge

public class ContactAndLeadSearch{
 public static List<List< SObject>> searchContactsAndLeads(String Smith){
 List<List<SObject>> searchList = [FIND 'Smith' IN ALL FIELDS 
                                      RETURNING Lead(LastName), Contact(LastName) ];
 
 return searchList ;
 }

}

Note: Create one Lead record and one Contact record in your Org having LastName = 'Smith' and then check the challenge.
Ravichandra sindheRavichandra sindhe

HI all
i am facing an error while inserting contact record.

could you please help me 
lead ad=new lead(firstname='jan',LastName='smith');

insert ad;dddd

Siva Sankar PaluriSiva Sankar Paluri
Hi 
Arunkumar R,

Thanks!.

While executing few classes in anonymous block, getting this error.  {static can only be used on methods of a top level type}

Can you please suggest why this occurs what we should to avoid this?
 
AnantGargAnantGarg
Do make sure you have created one 'Contact' and one 'Lead' with LastName='Smith' to complete this challenge. Under Debug open execute anonymous window and run
Lead led = new Lead(
    FirstName='Any',
    LastName='Smith',
    Company='myCompanyl');
insert led;
Contact cnt = new Contact(
    FirstName='Any',
    lastName='Smith');
insert cnt;
Rest you can follow any of the answer given here, all are correct.
Hunter AkersHunter Akers
Here's how it's done...
Pay close attention to LEAD...to execute the function a new lead has a "required" Company record. If you use the code I provided but you attempt to remove    ,Company='YourCompany'    it will give you and error upon attempting step #4. Try it and see...That's how I learned Lead requires Company to upsert. 

You must complete each step. If you do not debug->>execute the function it will not update the record and you'd have to do it manually as others suggest in the post above.

1st step: Insert this code

public class ContactAndLeadSearch {
    public static List<List<SObject>> searchContactsAndLeads(String name){
        Contact con = new Contact(LastName='Smith');
        upsert con;
        Lead le = new Lead(LastName='Smith',Company='YourCompany');
        upsert le;
        
        List<List<SObject>> searchList = [Find :name IN NAME FIELDS RETURNING Contact(FirstName,LastName), Lead(FirstName,LastName)];
        return searchList;
    }
}

2nd step: Save
3rd step: Debug->Open Execute Anonynmous Window: insert this code

ContactAndLeadSearch.searchContactsAndLeads('Smith');

4th step: Press Execute
5th step: You're done -> Check challenge to earn 500 points
Sangita07Sangita07

Hi

Can someone please explain the below code line 2 and 3?

List<List<sObject>> searchList =  [FIND 'Wingo OR SFDC' IN ALL FIELDS RETURNING Account(Name),Contact(FirstName,LastName,Department)];
Account[] searchAccounts = (Account[])searchList[0];
Contact[] searchContacts = (Contact[])searchList[1];

Ryll JohnRyll John
public class ContactAndLeadSearch {
    
    public static List<List<sObject>> searchContactsAndLeads(String incomingParam){
        List<List<sObject>> searchList = [FIND : incomingParam IN ALL FIELDS 
                   RETURNING Contact(FirstName, LastName),Lead(FirstName, LastName)];
        return searchList;
    }
    
}

above code is working.
Nitin Kumar SharmaNitin Kumar Sharma
hi All,
i see everyone has used "IN ALL FIELDS" as the SEARCH GROUP.
If I am reading it correct, the ask is to search in FIRST and LAST NAME only. Hence, we can try "IN NAME FILEDS" as the SEARCH GROUP.

I did that and it was accepted.. 
 
public class ContactAndLeadSearch {
    
    //Instead of ALL FIELDS, I AM USING NAME FIELDS TO BE PRECISE.
    public static list<list<sObject>> searchContactsAndLeads(String inputParameter){
        list<list<sObject>> myListObject =[ FIND :inputParameter
                                          	IN NAME FIELDS
                                           RETURNING Contact(FirstName,LastName),
                                           Lead(FirstName,LastName)
                                          ] ;
        return myListObject;
        
        
    }//searchContactsAndLeads() over..

}

 
Aathmacharan SelvasudarsananAathmacharan Selvasudarsanan
Thanks @ Isha Amoli, Your code is working great for clearing the challenge 
public class ContactAndLeadSearch{
 public static List<List< SObject>> searchContactsAndLeads(String Smith){
 List<List<SObject>> searchList = [FIND 'Smith' IN ALL FIELDS 
                                      RETURNING Lead(LastName), Contact(LastName) ];
 
 return searchList ;
 }

}
Thanks @ AnantGarg, For  giving simple way to creating records
Lead led = new Lead(
    FirstName='Any',
    LastName='Smith',
    Company='myCompanyl');
insert led;
Contact cnt = new Contact(
    FirstName='Any',
    lastName='Smith');
insert cnt;
Shabana NasirShabana Nasir
Hi dear friends, this is my first time commenting here. I am just a beginner. In the challenge there is a condition where the variable we are giving should be matched with first or last name of contacts and leads. I got the result and challenge completed when I wrote this code.
public class ContactAndLeadSearch
{
   public static List<List< SObject>> searchContactsAndLeads(string var1)
   {   
      List<List< SObject>> conts =  [FIND :var1 IN ALL FIELDS 
                   RETURNING Contact(FirstName,LastName WHERE FirstName = :Var1 OR LastName = :Var1),Lead(FirstName,LastName WHERE FirstName = :Var1 OR LastName = :Var1)];
      System.debug(conts);
      return conts;
   }
}
 
AAYUSHI SHRIVASTAVAAAYUSHI SHRIVASTAVA
You can use the code below:

public class ContactAndLeadSearch {
        public static List<List<SObject>> searchContactsAndLeads(String str)
        {
            Contact con = new Contact(LastName='Smith');
            upsert con;
            Lead le = new Lead(LastName='Smith',Company='YourCompany');
            upsert le;
            
          List<List<SObject>> searchList = [FIND 'str' IN ALL FIELDS 
                                      RETURNING Contact(FirstName,LastName), Lead(FirstName,LastName)];  
             return searchList;
        }
}

Thanks, Aayushi.
Rahul Gupta 317Rahul Gupta 317
 below code  works,,

public class ContactAndLeadSearch {
    public static List<List< SObject>>  searchContactsAndLeads(string searchText){
    List<List<sObject>> searchList = [FIND :searchText IN ALL FIELDS 
                                     RETURNING 
                                     Contact(FirstName,LastName),
                                     Lead(FirstName,LastName)];
        
              Contact[] searchContacts = (Contact[])searchList[0];
              Lead[] searchLeads = (Lead[])searchList[1];
        
              System.debug('found the following contacts.');
              for (Contact c : searchContacts) {
                System.debug(c.LastName + ', '+ c.FirstName);
            }
            
             System.debug ('Found the following Leads.');    
              for (Lead l : searchLeads) {
                 System.debug (l.LastName + ', ' + l.FirstName);
            }        
           
             return searchlist;    
  }
    
  }


Please mark helpful once done.
Thanks & Regards,
Rahul Gupta.
Sameer Macwan 7Sameer Macwan 7
Hello Folks,

This Worked for me I hope it will work for you guys as well.

(Before doing the below steps make sure you have created "Smith" as the 'Last Name' in 'Contact' as well as 'Lead')

Follow the steps:

Step 1: Go into the Developer Console > Apex class >ContactAndLeadSearch

Step 2: Follow the below syntax.

public class ContactAndLeadSearch {
public static list<list<sobject>> searchContactsAndLeads(string lname)
{
    list<list<sobject>> LS = [FIND :lname in Name Fields returning Lead(lastname,firstname where
                                                                         lastname =: lname or firstname =: lname
                                                                         ),
                                                                       contact(lastname,firstname where
                                                                       lastname =: lname or firstname =: lname)];

    return LS;
}
}


Step 3: For debug execution.

ContactAndLeadSearch.searchContactsAndLeads('Smith');

Thank you!!!

Sameer Macwan
 
Amol KastureAmol Kasture
Use following code...it will work sure
============================================================================================================

public class ContactAndLeadSearch {

    public static List<List<SObject>> searchContactsAndLeads(String FLname)

       {
       List<List<sObject>> searchList = [FIND :FLname IN ALL FIELDS RETURNING Contact(FirstName,LastName) ,Lead(FirstName,Lastname)];
       
       System.debug(searchList.size() + ' contacts & Leads are  returned.');
       return searchList;
      }
}
=============================================================================================================

Please mark helpful once done.
Thanks & Regards
Amol K
Apar JaggiApar Jaggi
// Heyy
//Try this


public class ContactAndLeadSearch {
    public static  List<List<sObject>> searchContactsAndLeads(String str){
        list<list<sObject>> lis=[FIND : str IN ALL FIELDS RETURNING Contact(FirstName,LastName),Lead(FirstName,LastName) ];
        return lis;
    }
}
Gonapati Bhavitha 15Gonapati Bhavitha 15
HI Rishabh,

try this

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

}

Anonymous Window
ContactAndLeadSearch.searchContactsAndLeads('smith');

​​​​​​​

Please mark it as a best answer!
Thanks!
Natalia BannikovaNatalia Bannikova
Hello! It works:
public class ContactAndLeadSearch {
    public static List<List< sObject>> searchContactsAndLeads(String name){
        return [FIND :name IN ALL FIELDS RETURNING Lead(FirstName,LastName),Contact(FirstName,LastName)];
    }
}
Shailaja GeelaShailaja Geela
Add new Contact and Lead with lastname 'smith'
create new class

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

Execute:
ContactAndLeadSearch.searchContactsAndLeads('smith');