You need to sign in to do that
Don't have an account?
Mike 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];
}
}
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];
}
}
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
public static List<List<SObject>> searchContactsAndLeads(String srch) {
List<List<SObject>> results = [FIND :srch IN NAME FIELDS];
return results;
}
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 :)
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!
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:
Hope this helps.
Fred
//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;
}
}
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;
}
}
public static List<List< SObject>> searchContactsAndLeads(string accn){
List<List<SObject>> searchList = [FIND :accn IN ALL FIELDS
RETURNING Lead(LastName), Contact(LastName)];
return searchlist;
}
}
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
*************************************************************************************************************************
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;
}
}
*********************************************************************************************************************************