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
ankit kadamankit kadam 

Getting Error in trailhead challenge

Hi,
i am getting following error.
"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 code below:
public class ContactAndLeadSearch {
    public static List<List<SObject>>ContactAndLeadSearch(String s1){
        
        List<List<sObject>> searchList = [FIND 's1' 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 leads.');
for (Lead a : searchLeads) {
    System.debug(a.Name);
}

System.debug('Found the following contacts.');
for (Contact c : searchContacts) {
    System.debug(c.LastName + ', ' + c.FirstName);
}
        return searchList;
    }
}

 
Best Answer chosen by ankit kadam
Amit Chaudhary 8Amit Chaudhary 8
Please check below post for solution. I hope that will help u
https://developer.salesforce.com/forums/?id=906F0000000BO5rIAG
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;
        }
}
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 let us know if this will help u


 

All Answers

sachin kadian 5sachin kadian 5
Hi Ankit,

If this is your complete class, i can see you do not have any method called searchContactsAndLeads() in this class. so the error is coming. Either create that method or rename ContactAndLeadSearch() to searchContactsAndLeads() . 
ankit kadamankit kadam
Hi Sachin,
Thanks for the reply, as u have mentioned , i have change the above method name from 
ContactAndLeadSearch() to searchContactsAndLeads().
But, still it's diplaying the same error.

And my updated code is looks like this:
public class ContactAndLeadSearch {
    public static List<List<SObject>> searchContactsAndLeads(String s1){
        
        List<List<sObject>> searchList = [FIND 's1' 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 leads.');
for (Lead a : searchLeads) {
    System.debug(a.Name);
}

System.debug('Found the following contacts.');
for (Contact c : searchContacts) {
    System.debug(c.LastName + ', ' + c.FirstName);
}
        return searchList;
    }
}


 
sachin kadian 5sachin kadian 5
Can you please paste the code where you are calling it.
ankit kadamankit kadam
i m just returning this list 
sachin kadian 5sachin kadian 5
I hope you will be calling it like this

List<List<SObject>> lst = ContactAndLeadSearch.searchContactsAndLeads('xyz');
ankit kadamankit kadam
sachin, i have the following code now and it was showing problem before executing "Error"


i m running below code now,
public class ContactAndLeadSearch {
    public static List<List<SObject>> searchContactsAndLeads(String s1){
        
        List<List<sObject>> searchList = [FIND 's1' 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 leads.');
for (Lead a : searchLeads) {
    System.debug(a.Name);
}

System.debug('Found the following contacts.');
for (Contact c : searchContacts) {
    System.debug(c.LastName + ', ' + c.FirstName);
}
        return searchList;
    }
    ContactAndLeadSearch.searchContactsAndLeads('xyz');
}

 
sachin kadian 5sachin kadian 5
No this is wrong. Why are you calling the same method in that method.  means why are you calling searchContactsAndLeads inside searchContactsAndLeads . Ok tell me when thing, when were you getting that error before calling this? means were you getting it while saving the class or doing something else?
sachin kadian 5sachin kadian 5
try to execute it from anonymous window in developer class (by pressing ctrl+e). dont write this line it this class. press ctrl+e on your developer console , it will open anonymous window. write ContactAndLeadSearch.searchContactsAndLeads('xyz'); there and press execute.
Amit Chaudhary 8Amit Chaudhary 8
Please check below post for solution. I hope that will help u
https://developer.salesforce.com/forums/?id=906F0000000BO5rIAG
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;
        }
}
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 let us know if this will help u


 
This was selected as the best answer
ankit kadamankit kadam
sachin, its working in the developer console, i have checked logs and it's displaying debug statements. But when i am checking the code on trailhead it's displaying the same error.

.User-added image


 
ankit kadamankit kadam
Thanks...... Sachin and Amit.. 
it's working fine now..

Thanks a lot to both of u.

Regards
Er. Ankit Kadam