+ 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.