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
Mike Kellum 10Mike Kellum 10 

Trouble with "Apex Basics & Database" challenges 3-5

Hello all,

I'm unable to complete the challenges for steps 3-5 in the "Apex Basics & Database" Trailhead module. I can't figure out what could be wrong with any of my classes. The message I get for all three is a variation on:

"Executing the 'searchContactsAndLeads' method failed. Either the method does not exist, is not static, or does not return the expected search results."

All of the classes are public. All of the methods are public and static. I've checked the return types and have successfully run them all with test data. I've tried re-entering the test data provided in the module to make sure it's accurate. No idea what I'm doing that makes this fail. (I'm not using a custom namespace.)

I'll paste my code below. Any tips would be greatly appreciated!

Thanks,
Mike

public class AccountHandler {
    public static ID insertNewAccount(String acctName) {
        Account newAcct = new Account(Name=acctName);
        try {
            insert newAcct;
        } catch (DMLException e) {
            System.debug('A DML exception has occurred: ' +
                         e.getMessage());
            return Null;
        }        
        return newAcct.Id;
    }
}

public class ContactSearch {
    public static List<Contact> searchForContact(
        String lastName, String postalCode) {
            List<Contact> contacts = new List<Contact>();
            contacts = [SELECT Name
                        FROM Contact
                        WHERE LastName = :lastName AND
                        MailingPostalCode = :postalCode ];           
            return contacts;
        }
}

public with sharing class ContactAndLeadSearch {
    public static List<List<SObject>> searchContactsAndLeads(String srch) {
        List<List<SObject>> results = [FIND :srch IN NAME FIELDS];
        }
}
Best Answer chosen by Mike Kellum 10
Ramanujam Varadhan 4Ramanujam Varadhan 4

For ContactSearch, can you try adding Id as one of the query items? I dont remember the question very well but I think that the system could be looking for the Id. The following code which I had written, worked fine.

public class ContactSearch{
    public static List<Contact>searchForContacts(String lastName,String mailingPostalCode){
        List<Contact> contactList=[SELECT Id,FirstName,LastName,MailingPostalCode FROM Contact WHERE LastName=:lastName AND MailingPostalCode=:mailingPostalCode];
            return contactList;
            }
}

For AccountHandler, the following code worked.

public class AccountHandler{
    public static Account insertNewAccount(String accountName){  
        Account acctToBeInserted = new Account(Name=accountName);
      
        try{
            insert acctToBeInserted;
        }catch(DMLException e){
            System.debug('Inside DMLException catch ,error is ' + e.getMessage());
            acctToBeInserted = NULL;   
            }
       return acctToBeInserted;                 
       }
}

 

I have nullified the account variable inside the catch and used a common return statement in the end.Worth a try :)

All Answers

Roy LuoRoy Luo
Missing return statement:

public static List<List<SObject>> searchContactsAndLeads(String srch) {
        List<List<SObject>> results = [FIND :srch IN NAME FIELDS];
        return results;
        }

 
Mike Kellum 10Mike Kellum 10
Thanks for the catch. Looking back I can see the third one isn't quite ready for prime time. (: I'm still lost on the other two, though.
Ramanujam Varadhan 4Ramanujam Varadhan 4

For ContactSearch, can you try adding Id as one of the query items? I dont remember the question very well but I think that the system could be looking for the Id. The following code which I had written, worked fine.

public class ContactSearch{
    public static List<Contact>searchForContacts(String lastName,String mailingPostalCode){
        List<Contact> contactList=[SELECT Id,FirstName,LastName,MailingPostalCode FROM Contact WHERE LastName=:lastName AND MailingPostalCode=:mailingPostalCode];
            return contactList;
            }
}

For AccountHandler, the following code worked.

public class AccountHandler{
    public static Account insertNewAccount(String accountName){  
        Account acctToBeInserted = new Account(Name=accountName);
      
        try{
            insert acctToBeInserted;
        }catch(DMLException e){
            System.debug('Inside DMLException catch ,error is ' + e.getMessage());
            acctToBeInserted = NULL;   
            }
       return acctToBeInserted;                 
       }
}

 

I have nullified the account variable inside the catch and used a common return statement in the end.Worth a try :)

This was selected as the best answer
Mike Kellum 10Mike Kellum 10
Thanks for the reply! The ID is returned on SOQL queries even if you don't ask for it, I think. It shows up when I run a test form my anonymous window. I tried adding it explicitly, but no luck getting the challenge to pass.

I did however see that my method was called "searchForContact" instead of "searchForContacts." Looks like that was the problem. How silly!

Your code also helped me resolve the second one. I had the wrong return type. I was returning the ID instead of the Account itself--I noticed when I compared to yours. Thanks!
Frédéric TrébuchetFrédéric Trébuchet
Hello,

I'd the same problem and solved it by replacing 'AND' by 'OR' in the query. It make sense as the method have to return a list of contacts.
Here is my code:
public class ContactSearch {
	public static List<Contact> searchForContacts(String contactName, String contactPostCode) {
		List<Contact> contacts = [SELECT Name 
                                  FROM Contact 
                                  WHERE Name=:contactName 
                                  OR MailingPostalCode=:contactPostCode];
        return contacts;
    }
}

Hope this helps.
Fred
Kishan MalepuKishan Malepu
public class ContactSearch {
//Create an Apex class that returns contacts based on incoming parameters.
    public static List<Contact> searchForContacts(String Name, String Postalcode){    
        List<Contact> contacts = [select name , id from contact 
                                  where LastName =:Name OR MailingPostalCode =:Postalcode];
        system.debug(contacts);
            return contacts;
    }
  }
Jyotirmoy MondalJyotirmoy Mondal
Try out the following:

public class ContactAndLeadSearch {
    public static List<List<sObject>> searchContactsAndLeads(String searchCriteria){
        List<List<sObject>> searchResult  = [FIND :searchCriteria  IN ALL FIELDS
                                            RETURNING Contact(FirstName, LastName, Account.Name WHERE (FirstName =: searchCriteria OR LastName =: searchCriteria)), 
                                            Lead(FirstName, LastName, Company WHERE (FirstName =: searchCriteria OR LastName =: searchCriteria))];
        Return searchResult;
    }
}
MM SaikumarMM Saikumar
public class ContactAndLeadSearch {
    public static List<List< SObject>> searchContactsAndLeads(string accn){
        List<List<SObject>> searchList = [FIND :accn  IN ALL FIELDS 
                                      RETURNING Lead(LastName), Contact(LastName)];
        return searchlist;
    }
}
shashikant pandeyshashikant pandey

Copy and past the below code, it will work.
public class ContactAndLeadSearch {
        
    public static List<List<SObject>> searchContactsAndLeads(String check){
       list<List<SObject>> searchstring = [FIND :check  IN ALL FIELDS RETURNING Lead(LastName), Contact(LastName)];
           return searchstring;  
    }
}

Thanks
pratik kedar 14pratik kedar 14
I passed that challenge with below code
*************************************************************************************************************************
public class ContactSearch {

    Public static List<Contact> searchForContacts(String lName, String mCode){
        List<Contact> qContact = [select id, firstName, lastName, phone from contact
                                         where lastName =: lName AND MailingPostalCode =: mCode ];
return qContact;      
    }
}

*********************************************************************************************************************************