You need to sign in to do that
Don't have an account?
OU VISAL
Error on Trailhead 'ContactSearch' Apex class
Hello,
I alway get error "Challenge not yet complete... here's what's wrong:
Executing the 'searchForContacts' method failed. Either the method does not exist, is not static, or does not return the expected contacts."
I have my code as following:
public class ContactSearch {
public List<Contact> searchForContacts (String cName, String pCode) {
List<Contact> matchCon = [SELECT Id, First_Name__c, Last_Name__c, MailingPostalCode__c FROM Contact
WHERE Last_Name__c = 'cName'
AND MailingPostalCode__c = 'pCode'];
if(matchCon.size() > 0){
return matchCon;
} else {
return null;
}
}
}
Kindy check and tell me what is wrong
thank in advance
Visal
I alway get error "Challenge not yet complete... here's what's wrong:
Executing the 'searchForContacts' method failed. Either the method does not exist, is not static, or does not return the expected contacts."
I have my code as following:
public class ContactSearch {
public List<Contact> searchForContacts (String cName, String pCode) {
List<Contact> matchCon = [SELECT Id, First_Name__c, Last_Name__c, MailingPostalCode__c FROM Contact
WHERE Last_Name__c = 'cName'
AND MailingPostalCode__c = 'pCode'];
if(matchCon.size() > 0){
return matchCon;
} else {
return null;
}
}
}
Kindy check and tell me what is wrong
thank in advance
Visal
@Visal:
"Executing the 'searchForContacts' method failed. Either the method does not exist, is not static, or does not return the expected contacts." has your answer, the method you defined is not static.
Make it:
public Static List<Contact> searchForContacts (String cName, String pCode)
and also in the query, instead of using variable names, you had given them in quotes which make them string., change query to
List<Contact> matchCon = [SELECT Id, First_Name__c, Last_Name__c, MailingPostalCode__c FROM Contact
WHERE Last_Name__c = :cName
AND MailingPostalCode__c = :pCode];
Hope it helps.,
Thanks,
balaji
thank you for your reply.
The method should return a list of Contact records with at least the ID and Name fields. This is the requirement, so the method must not be static.
Kindly refer to below link to original problem.
https://developer.salesforce.com/trailhead/force_com_programmatic_beginner/apex_database/apex_database_soql
regards,
Visal
thanks in advance
In the challange it only says "find same last name and same postal code", so why not useing
but useing
So we have to create new custome field inorder to pass this challenge?
public class ContactSearch {
Public Static List<Contact> searchForContacts(String strLName,String strMailingPostalCode ){
List<Contact> lstCont = [Select ID, Name from Contact Where LastName =:strLName And MailingPostalCode =: strMailingPostalCode ];
Return lstCont;
}
}
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;
}
}
*******************************************************************************
public class ContactSearch {
public static List<Contact> searchForContacts(String m,String n){
List<Contact> con= [Select Id,Name from Contact where (LastName =:m AND MailingPostalCode =:n)];
return con;
}
}
public class ContactSearch {
public static List<Contact> searchForContacts(String lastName, String postalCode) {
List<Contact> matchingContacts = [
SELECT Id, Name
FROM Contact
WHERE LastName = :lastName AND MailingPostalCode = :postalCode
];
return matchingContacts;
}
}