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
Sandeep YadavSandeep Yadav 

Write an Inline SOSL Search to Return Database Values

On execution this in my Anonymous window --

List<List<sObject>> searchList = [FINd 'Mission Control' in ALL Field
                                   RETURNING Contact(FirstName,LastName,Email,Phone,Department)];

 Contact[] searchContacts = (Contact[])searchList[0];
    System.debug(searchContacts[0].LastName + ' ,' + searchContacts[0].FirstName);

a PopBox says that-
Unexpected Token at Line 1

 
Best Answer chosen by Sandeep Yadav
Dushyant SonwarDushyant Sonwar
Hi Sandeep ,

You are not putting the system.debug correctly for the challenge.
Please try with below code. This will work fine
List<List<sObject>> searchList = [FIND 'Mission Control' IN ALL FIELDS 
                                  RETURNING Contact(FirstName, LastName,
                                  Phone, Email, Description)];

Contact[] searchContacts = (Contact[])searchList[0];

System.debug(searchContacts[0].LastName + ', ' + searchContacts[0].FirstName);


Reason :
You have put two extra spaces  in comma . Change below to
System.debug(searchContacts[0].LastName + ' , ' + searchContacts[0].FirstName);

to
System.debug(searchContacts[0].LastName + ', ' + searchContacts[0].FirstName);



Your debug line is causing the challenge not to be completed.
Happy TrailBlazing :)

All Answers

Dushyant SonwarDushyant Sonwar
Hi Sandeep,

Your SOSL has typo error(Field) . It will be 'Fields'. Change this below SOSL 
List<List<sObject>> searchList = [FINd 'Mission Control' in ALL Field
                                   RETURNING Contact(FirstName,LastName,Email,Phone,Department)];
to

List<List<sObject>> searchList = [FINd 'Mission Control' in ALL Fields
                                   RETURNING Contact(FirstName,LastName,Email,Phone,Department)];

and you are good to go :)
Sandeep YadavSandeep Yadav
Thanks Dushyant for Pointing
But i still get an error--
"Challenge Not yet complete... here's what's wrong: 
Could not find the contact's name in the debug log. Be sure to run a query for your record, and to write your contact's name to the debug log using the System.debug() method."

In my debug log i can see contact's first and last name
Dushyant SonwarDushyant Sonwar

Are you doing this trailhead module?
https://trailhead.salesforce.com/en/modules/apex_database/units/apex_database_sosl
 
public class ContactAndLeadSearch{
    public static List<List< SObject>> searchContactsAndLeads(string nm){
        List<List<SObject>> searchList = [FIND :nm IN ALL FIELDS 
                                      RETURNING Lead(FirstName,LastName),Contact(FirstName,LastName)];
        return searchList;                              
    }
}
If yes , then may be you are not fetching  the lead. Please try using above code and let us know.
 
Dushyant SonwarDushyant Sonwar
Sandeep , You are getting error in check challenge because your debug statement is missing the spaces. You have put space before ' '
It should be ', '

Change
System.debug(searchContacts[0].LastName + ' ,' + searchContacts[0].FirstName);

to
System.debug(searchContacts[0].LastName + ', ' + searchContacts[0].FirstName);

 ​Hope this helps :)
Sandeep YadavSandeep Yadav
Same error
Challenge Not yet complete... here's what's wrong: 
Could not find the contact's name in the debug log. Be sure to run a query for your record, and to write your contact's name to the debug log using the System.debug() method.

here what I did after your suggestion--
List<List<sObject>> searchList = [FINd 'Mission Control' in ALL Fields
                                   RETURNING Contact(FirstName,LastName,Email,Phone,Department)];

 Contact[] searchContacts = (Contact[])searchList[0];

    System.debug(searchContacts[0].LastName + ' , ' + searchContacts[0].FirstName);


 
Dushyant SonwarDushyant Sonwar
Hi Sandeep ,

You are not putting the system.debug correctly for the challenge.
Please try with below code. This will work fine
List<List<sObject>> searchList = [FIND 'Mission Control' IN ALL FIELDS 
                                  RETURNING Contact(FirstName, LastName,
                                  Phone, Email, Description)];

Contact[] searchContacts = (Contact[])searchList[0];

System.debug(searchContacts[0].LastName + ', ' + searchContacts[0].FirstName);


Reason :
You have put two extra spaces  in comma . Change below to
System.debug(searchContacts[0].LastName + ' , ' + searchContacts[0].FirstName);

to
System.debug(searchContacts[0].LastName + ', ' + searchContacts[0].FirstName);



Your debug line is causing the challenge not to be completed.
Happy TrailBlazing :)
This was selected as the best answer
Sandeep YadavSandeep Yadav
Thanks Dushyant
Its very deeply observation
Parikhit SarkarParikhit Sarkar
Thanks dushyant , I was stuck with this problem for a while.