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 

Writing SOSL query trailhead challenge

I'm stuck on the SOSL query challenge in trailhead. can't write the method.

cheers
Himanshu ParasharHimanshu Parashar
what is the error you are getting ?
MubashirMubashir
Hi Himanshu,

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 a lot
António Pedro Melo AlvimAntónio Pedro Melo Alvim
Hello Mubashir, I'm Still trying to complete the challenge so I still do not have the final answer, nevertheless I noticed that the challenge indicates: 
The return type for 'searchContactsAndLeads' must be 'List<List< SObject>>'

You have two returns in a single method, which will not work - you should return a single collection of lists. 

Cheers, 
MubashirMubashir
Thanks Antonio,
How do i return a collection though?

cheers
António Pedro Melo AlvimAntónio Pedro Melo Alvim
Hi, from what I see i would change two things - 

1. change the method declaration to have something like this - a collection of collections.
public static List<List< SObject>> searchContactsAndLeads(String input){

// I've included in Bold and Italic the return type - simply use this instead of an array like you had in your example.

2. Change the return - You will only have to return the "searchResults" variable you have in your code. 
Something like
return searchResults;

notes -
a. You declare a collection of collections in a similar way to a normal collection - the trailhead Apex Basics & Database module on section Writing SOSL Queries, has an example for your reference (Search for "SOSL Apex Example"), and you can find more examples in Salesforce documentation.
b. I found a cool tutorial on Java, not Apex (but I'm guessing the behavior similar) on the "return" keyword here:
http://www.java-samples.com/showtutorial.php?tutorialid=280
c. From what I understood you don't have to get the individual collections to get the challenge successfully evaluated. 
MubashirMubashir
Thanks again Antonio,
Hope you don't mind carrying on the conversation.

Here's what I get for 'searchResults' - "Return value must be of type List", but to me it is a List.

User-added image

cheers
Himanshu ParasharHimanshu Parashar
Hi Mubashir,

The Problem is with return type when you have List<List<sObject>> you need to define function return type the same.
 
public class ContactAndLeadSearch{

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

Thanks,
Himanshu
Salesforce Certified Developer | Administrator | Service Cloud Consultant

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.
António Pedro Melo AlvimAntónio Pedro Melo Alvim
Hello again - no worries: I sense that you are mixing "lists" and "arrays". In your code line 6 you have an array declared as indicated by the usage of [], but you are returning a List as indicated by the <> (line 14). The list is initialized in line 10. 

You can read a bit more here: https://developer.salesforce.com/forums/ForumsMain?id=906F00000008zveIAA

Hope this helps!
MubashirMubashir
Brilliant, thanks a mil both of you Himanshu and Antonio. Yes I had to declare List instead of an Array. I've completed the challenge now. 

But there's another trouble now: who do I give 'Best Answer' as only one can be done - oops! a tie!
Himanshu ParasharHimanshu Parashar
ha ha.. it's your choice the thing matter is we are able to help you. ;)
 
António Pedro Melo AlvimAntónio Pedro Melo Alvim
hehe :) Don't worry about it, glad that we could help. 
Himanshu ParasharHimanshu Parashar
Hi Namath,

You need to follow this url to create contact record

http://na3.salesforce.com/003/e and

https://na3.salesforce.com/00Q/e 

(replace na3 with your salesforce instance)

Thanks,
Himanshu
Lynda ElbayLynda Elbay
Hello, 
You have to create new lead reccord with last name = Smith and also convert it without opp to have a contact ! 

public with sharing class ContactAndLeadSearch {
    public static List<List<SObject>> searchContactsAndLeads(string param){
    List<List<Sobject>> searchConLead = [FIND: param IN all fields returning Lead(FirstName, LastName),Contact(FirstName,LastName)];
    return (searchConLead);
   }
  }

Hope it help :)
 
Priyanka SahooPriyanka Sahoo
Click on Home tab and Create Lead and Contact record with LastName=Smith as shown below:
User-added image
 

Add the below in the Apex class:

public class ContactAndLeadSearch {
    public static List<List< SObject>>searchContactsAndLeads( String str){    
     List<List<SObject>> searchList = [FIND :str  RETURNING Contact(FirstName, LastName), Lead(FirstName , LastName)];
        return searchList;
    }
}
David RossDavid Ross
This was the solution I used and it worked, 

public class ContactAndLeadSearch {
    public static List<List< SObject>> searchContactsAndLeads(string smith){    
          List<List<sObject>> searchList = [FIND 'Smith' IN ALL FIELDS 
                                            RETURNING Lead(LastName WHERE LastName = :'smith'),Contact(LastName Where LastName = :'smith')];
return (searchList);
        
   }
}
sandesh gaikwad 27sandesh gaikwad 27
This Worked for me :

public class ContactAndLeadSearch {

    public static List<List<sObject>> searchContactsAndLeads(String Srchstring)
    {
        List<List<sObject>> SrcList=[Find :Srchstring IN all FIELDS  returning Contact(Firstname,LastName), Lead(Firstname,LastName)];
        return SrcList;
    }
}
Sasank LokaSasank Loka
Why the below code is not passing the challenge?

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

}
Stanislav KováříkStanislav Kovářík
Hi Sasank,

it's probably because every record in Salesforce is natively represented as an sObject in Apex, not just 'Object'.
The rest of the code seems OK. :-)
Mangal Gupta 10Mangal Gupta 10
Hi Everyone,

The below code is working absolutely fine.

public class ContactAndLeadSearch {
    
    public static List<list<sObject>> searchContactsAndLeads (String First){
        List<list<sObject>> searchdata = new List<list<sObject>>();
        
        searchdata =[Find :First IN ALL Fields RETURNING Contact(Firstname,Lastname),Lead(Firstname,Lastname)];
        system.debug('@@@@'+searchdata);
        Return searchdata;
        
        
    }
    

}
hit_nishanthit_nishant
public class ContactAndLeadSearch {
    public static List<List<SObject>> searchContactsAndLeads(string param)
    {
    List<List<Sobject>> searchConLead = [FIND: param IN all fields returning Lead(FirstName, LastName),Contact(FirstName,LastName)];
    return (searchConLead);
   }
}
/*
before calling this method, execute below through anonymous window first

Account acct = new Account (
            Name = 'Smith Computing',
            Phone = '(415 555-1212',

            NumberOfEmployees = 50,

            BillingCity = 'San Francisco');
        insert acct;
        ID acctID = acct.ID;
      
       Contact con = new Contact (
            FirstName = 'Carol',
            LastName = 'Smith',
            Phone = '(415) 555-1212',
            Department = 'Wingo',
            AccountId = acctID);
        insert con;
        
        Lead theLead = new Lead(
            FirstName = 'Jim',
            LastName = 'Smith',
            Phone = '(111) 111-1111',
            Company = 'IGATE',

            LeadSource = 'Web');
        insert theLead;

 */ 
 
hashir kphashir kp
This Worked For Me

public class ContactAndLeadSearch {
    public static List<List<SObject>> searchContactsAndLeads(string userVariable){
        List<List<SObject>> result= [FIND :userVariable IN ALL FIELDS Returning Contact(FirstName,LastName), Lead(FirstName,LastName)];
        return result;
    }
}
 
Robert Griffin 3Robert Griffin 3
Super. I love useful discussions in which you can find answers to exciting questions. This is very valuable, especially when you need to solve a problem quickly and do not know where to turn. In one of these discussions, I found a site recommendation https://studentshare.org/capstone-project with examples of the Capstone Project, I was so happy, it was a timely find that solved some of my problems at the university, I am sharing it with you, maybe someone will also be relevant.
Naheed KhanNaheed Khan
Contact thisContact = new Contact(
    FirstName = 'Brian',
    LastName = 'Dent',
    Phone = '(619) 852-4569',
    Department = 'Mission Control',
    Title = 'Mission Specialist - Neptune',
    Email = 'briandent@trailhead.com');
insert thisContact;    
    
    List<List<sObject>> searchList = [FIND 'Mission Control' IN ALL FIELDS
                                  RETURNING Contact(FirstName, LastName,
                                  Phone, Email, Description)];
Contact[] searchContacts = (Contact[])searchList[0];
System.debug('Found the following contacts:');
for (Contact c : searchContacts) {
   System.debug(c.LastName + ', ' + c.FirstName);
    
    
}
Nishant GoyalNishant Goyal
Kindly Guide Whats is wrong in the code as I'm new to this platform.


Contact[] Contacts = [FIND {Mission Control} in all fields RETURNING Contact(FirstName, LastName)];
String theseContacts = Contacts.FirstName + ' ' + Contacts.LastName;
System.debug(theseContacts);