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
OU VISALOU 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
Balaji Chowdary GarapatiBalaji Chowdary Garapati

@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
OU VISALOU VISAL
Hello 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
Balaji Chowdary GarapatiBalaji Chowdary Garapati
Hello Visal, I think you confused static for void, Coming to the challenge, " The Apex class must have a public static method called 'searchForContacts'." This is the second point in the challenge, take a moment to read it! Thanks, Balaji
OU VISALOU VISAL
Oup, you are right! error from my side. Thank you again. Good weekend to you.
OU VISALOU VISAL
I found the solution and passed this challenge. thank you
Krithika DharmarajanKrithika Dharmarajan
Can anyone help on how exception should be handled for this problem ? when the string is empty ?
Himanshu NimjeHimanshu Nimje
can you tell me why you are using field name as firstname__c,lastname__c,MailingPostalCode__c ?
thanks in advance 
陳蕙心 Cymbi Chen陳蕙心 Cymbi Chen
Same with @Himanshu Nimje,
In the challange it only says "find same last name and same postal code", so why not useing
Name like '%targetLastName' and MailingAddress like 'targetPostalCode%'

but useing
[SELECT Id, First_Name__c, Last_Name__c, MailingPostalCode__c FROM Contact
                                  WHERE Last_Name__c = :cName
                                  AND MailingPostalCode__c = :pCode];

So we have to create new custome field inorder to pass this challenge?
MANOJ CHAUHAN 1MANOJ CHAUHAN 1
****Here is the Code*****
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;
    }
}
pratik kedar 14pratik kedar 14
Dear Hunter's 
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;      
    }
}
*******************************************************************************



 
Saquib MansuriSaquib Mansuri
Try this code. It worked for me

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;
    }
}
André HenriqueAndré Henrique
I used the following code and it worked.


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