+ Start a Discussion
Nagesh B 13Nagesh B 13 

Unable to pass the challenge in trialhead for below issue

Hi all,

i have below requirement in trialhead.

For this challenge, you will need to create a class that has a method accepting two strings. The method searches for contacts that have a last name matching the first string and a mailing postal code (API name: MailingPostalCode) matching the second. It gets the ID and Name of those contacts and returns them.
The Apex class must be called 'ContactSearch' and be in the public scope.
The Apex class must have a public static method called 'searchForContacts'.
The 'searchForContacts' method must accept two incoming strings as parameters, find any contact that has a last name matching the first, and mailing postal code matching the second string. The method should return a list of Contact records with at least the ID and Name fields.
The return type for 'searchForContacts' must be 'List<Contact>'.

i have written below code, but when i check challenge it is giving error.  like
'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.

'Am i doing anything wrong?  please correct me or guide on this.

public class ContactSearch {
    
    public static List<Contact> searchForContacts(String sname,String postalcode)
    {
        List<Contact> Clist;
       
      insert new Contact[]{new Contact(LastName='aint',MailingPostalCode__c='5600069'),
            new Contact(LastName='aria',MailingPostalCode__c='5700085')};
       
      clist=[select Id,Name from Contact Where  LastName=:sname and MailingPostalCode__c=:postalcode];
        return clist;
    }
}

Thanks,
Nag.
Abhi_TripathiAbhi_Tripathi
Is you static resource have only one file ?
 
Nagesh B 13Nagesh B 13
i didn't get you what exactly you are asking? please could u expand?
Abhi_TripathiAbhi_Tripathi
Sorry that was the reply of other question,

You can try below code for you Trailhead module, you have to use standard field MailingpostalCode
 
public class ContactSearch {
    
    public static List<Contact> searchForContacts(String sname,String postalcode)
    {
       
       List<Contact> clist = [select Id,Name from Contact Where  LastName=:sname and MailingpostalCode=:postalcode];
        return clist;
    }
}

 
JyothsnaJyothsna (Salesforce Developers) 
Hi Nagesh,

Please check the below code.
public class ContactSearch {
    public static List<contact> searchForContacts(string n, string p){
        List<contact> conlst=new List<contact>();
        conlst=[select id,Lastname from contact where Lastname=:n AND MailingPostalCode=:p];
        system.debug(conlst);
        return conlst;
    }
}

Open Execute anonymous window and you can pass lastname and postalcode.
ContactSearch.searchForContacts('sai','500072');

Hope this helps you!
Best Regards,
Jyothsna
Nagesh B 13Nagesh B 13
Thanks Abhi and Jyothsna. it was worked. But i have one doubt that there was only Mailing Address field on contact. why i am unable to see 'MailingPostalCode' standard field on contact object.
DeepthiDeepthi (Salesforce Developers) 
Hi Nagesh,

Address fields have long been a part of Salesforce, provided on many standard objects, such as Account, Contact, Quote, and User. Addresses are available as individual fields on the object, consisting of:
  • City
  • Country
  • CountryCode (when state and country picklists are enabled)
  • Latitude (beta)
  • Longitude (beta)
  • PostalCode
  • State
  • StateCode (when state and country picklists are enabled)
  • Street
Some objects provide fields for multiple addresses. For example, Account provides for four different addresses. In this case, address field names are prefixed with the type of address, for example, BillingCity, MailingState, and so on.

Hope this helps you!
Best Regards,
Deepthi
Nagesh B 13Nagesh B 13
Thanks Jyothsna.
Nagesh B 13Nagesh B 13
Thanks Deepti.