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
Lantin MaryLantin Mary 

Trailhead Challenge: Write an Inline SOSL Search to Return Database Values

The challenge is to search for the inserted record with an inline SOSL search, using Execute Anonymous.

This is what I did using Execute Anonymous Window
 
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+'"');
}

But still there's an error when I check the challenge.

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.
Best Answer chosen by Lantin Mary
Dhanya NDhanya N
Hi Lantin,

Try this :
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);
It worked for me.

Thanks,
Dhanya
 

All Answers

NagendraNagendra (Salesforce Developers) 
Hi Mary,

Please try with 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;
        }
}
NOTE: If you want to search with same keyword in multiple fields then don't add where you can try IN ALL FIELDS.

Execute below code in In Debug Annonymous window.
List<List<sObject>> searchContactLead = ContactAndLeadSearch.searchContactsAndLeads('amit');

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 Please let us know if this will help you.

Best Regards,
Nagendra.

 
Lantin MaryLantin Mary
I still got the 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.
Dhanya NDhanya N
Hi Lantin,

Try this :
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);
It worked for me.

Thanks,
Dhanya
 
This was selected as the best answer
Ashok N 8Ashok N 8
Hi Lantin,
Yes, try Dhnaya's code. it works.
Thanks
Ashok
 
Gene BidwellGene Bidwell
I still get the 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.
Gene BidwellGene Bidwell
I got it to work. I had to log into the trailhead playground
Lantin MaryLantin Mary
Thank you Dhanya, it worked!
Alvin BernardAlvin Bernard
The trick to above is to make sure that we have an extra space after comma ", ".
If you don't have that one extra space the test will fail. No need to login into different org / playground.

//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>> c=[FIND 'Mission Control'IN ALL FIELDS RETURNING Contact(FirstName, LastName)];
Contact [] contacts = ((List<Contact>)c[0]);
System.debug(contacts[0].LastName+','+contacts[0].FirstName);
Andrew EversleyAndrew Eversley
Thanx to Dhanya N. That code worked great for me. 
Naveen IlaNaveen Ila
I tried the same, However no luck
Jayanth ThathapudiJayanth Thathapudi
@Naveen Ila

Try to login into another org and then check the Challenge
Just tried today..It worked out
LindaKatLindaKat
I tried all the solutions - Except logging in to another org - and nothing works.
Chad Evans WinchesterChad Evans Winchester
If you have done many other trails, you might have something else conflicting with the logs. I had a Transaction Security policy enabled that was creating a new log each time Trailhead queried the logs, and that was being returned instead of the execute annoymous results, causing a failure. Just had to disable that policy, and the solution above worked.
PAAPAA
Below code also works fine. Tried and Tested.

 
List<List<sObject>> searchList = [find 'Mission Control' IN ALL FIELDS
                                 RETURNING Contact(FirstName, LastName, Phone, Email, Description)];
List<Contact> searchContact = searchList.get(0);
System.debug('Found the following contacts:');
for(Contact c : searchContact){
    System.debug(c.lastName+', '+c.firstName);
}

 
Shailendra Singh 69Shailendra Singh 69
Thanks Chad,  It was Transaction Security issue. Disabled it and it worked.
M ParnellM Parnell
Thanks Dhanya! Your solution worked for me.
Sandeep YadavSandeep Yadav
i am still getting an error unexpected token ( < )
List<List<sObject>> sl = [-------]
thirumoorthy kandasamythirumoorthy kandasamy
', ' just a space between the apostrophe and comma  
Kevin Every 4Kevin Every 4
I had to delete my transaction security poilicies and it worked! Thanks Chad!
Ilya IvanovskiyIlya Ivanovskiy
step 1.
insert and run the code that adds a new contact, which we will then look for.
 
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;

step 2.
after the contact is added to our database, we make a request for a posik. Copy this code and run it.
 
List<List<sObject>> searchList = [find 'Mission Control' IN ALL FIELDS
                                 RETURNING Contact(FirstName, LastName, 
                                           Phone, Email, Description)];
List<Contact> searchContact = searchList.get(0);
System.debug('Found the following contacts:');
for(Contact c : searchContact){
    System.debug(c.lastName+', '+c.firstName);
}

step 3.
We check the execution of the task, everything should work.
wroxtarwroxtar
This worked for me. The mystery is the 'comma' and the 'space' after that---> lastName +', '+firstName !!!
List<List<Contact>> searchList = [FIND 'Mission Control' IN ALL FIELDS RETURNING
                                 Contact(FirstName, LastName, Phone, Email, Description)];
List<Contact> searchCon = (List<Contact>)searchList[0];

System.debug('Found the Control Engineer on Neptune:');
for(Contact c : searchCon){
    System.debug(c.LastName+', '+c.FirstName);
}


 
ashutosh sahoo 10ashutosh sahoo 10
# First Insert the Contact With The Given DataThrough Anonymous Window.
Then Clear Whole Window And Execute Only This

List<List<sObject>> con = [FIND 'Mission Control'  IN ALL FIELDS RETURNING Contact(FirstName,LastName)];
Contact[] conn = (Contact[])con[0];
for(Contact co:conn){
    system.debug(co.LastName+', '+co.FirstName);
//Don't forget to put a space after " , "
}
Dharti ShahDharti Shah
1. open developer console
2. debug > open execute annonymous window
3. enter the code-snippet below
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;
//System.debug('Found the following contact');
    System.debug(thisContact.Lastname + ', ' + thisContact.Firstname)
;
4. execute
5. can checkmark 'Debug only' checkbox to see the return.
 
lakshmikanth saikumarlakshmikanth saikumar
Basically, you should display the lastname,<space>Firstname. This will work.
I completed the challenge with below code.
 
list<list<sObject>> neptuneContact = [find 'Mission Control' in all fields
                                     returning contact(FirstName,LastName)];

contact[] cont = (contact[]) neptuneContact[0];
system.debug(cont[0].Lastname + ',  ' + cont[0].FirstName);

 
Jacob ChambersJacob Chambers
Thanks for the help!

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);
Kunal Bhargava 7Kunal Bhargava 7
hie,
Try this 

First, you have to Select Debug | Open Execute Anonymous Window. 
Copy this code & Execute - 

​​​​​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;

After the first step does this:
Select Debug | Open Execute Anonymous Window.
Copy this code & Execute - 

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);
}

This worked for me.
 
Aruna YadavAruna Yadav
Thanks Dhanya! Your solution worked for me.
Baskaran ArumugamBaskaran Arumugam

For me not working..
I got below error:

Incompatible types since an instance of List<SObject> is never an instance of List<contact>

Devis JenifferDevis Jeniffer
McAfee is a great antivirus program that has the ability to shield your computer system and mobile devices from all types of cyber threats such as viruses, malware, spyware, and all other unwanted programs. Anyone can easily install and download this antivirus software on their mobile devices and computer system. With the optimization tools that come within this antivirus software, it scans your devices in a much proper way and makes them free from all the virus attacks.

https://mcafeeactivateretailcardkey.yahoosites.com
CEZARY PAWCEZARY PAW
Ali UbaidAli Ubaid
I am still getting an error. it doesnt worked for me.
https://testdepurete.info