+ Start a Discussion
farah sheriffarah sherif 

hello beautiful developers I need help with this class

Create an Apex class that returns contacts based on incoming parameters.
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 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 method must accept two incoming strings as parameters
The method should then find any contact that has a last name matching the first string, and mailing postal code (API name: MailingPostalCode) matching the second string
The method should finally return a list of Contact records of type List that includes the ID and Name fields



below is my code 

public class ContactSearch {
    
    public static List<String> searchForContacts(String x, String y){
        x1=x;
        y1=y;
        List<String> con = new List<String>();
        for (Contact[] tmp : [SELECT Id,Name FROM Contact where Name := x1 AND MailingPostalCode :=y1]) {
            con.add(tmp);
        }
        return con;
       
    }

}
Best Answer chosen by farah sherif
Dinesh GopalakrishnanDinesh Gopalakrishnan
Hi Farah,

Please find the below Class.

public class ContactSearch {
    public static List<Contact> searchForContacts(String Last_Name,String Postal_Code){
        List<Contact> Contact_List= new List<Contact>();
        Contact_List = [SELECT Id,Name FROM Contact WHERE LastName = :Last_Name AND MailingPostalCode = :Postal_Code];
        return Contact_List;
    }
}

Thanks
Dinesh

All Answers

Dinesh GopalakrishnanDinesh Gopalakrishnan
Hi Farah,

Please find the below Class.

public class ContactSearch {
    public static List<Contact> searchForContacts(String Last_Name,String Postal_Code){
        List<Contact> Contact_List= new List<Contact>();
        Contact_List = [SELECT Id,Name FROM Contact WHERE LastName = :Last_Name AND MailingPostalCode = :Postal_Code];
        return Contact_List;
    }
}

Thanks
Dinesh
This was selected as the best answer
farah sheriffarah sherif
thank you so much Dinesh. can I have your email?