1

I'm trying to finish a challange of the Write SOQL Queries Trailhead Unit. Problem description says:

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.

I created an Apex class:

public class ContactSearch {
    public static List<Contacts> searchForContacts(String lastname, String Postalcode){
        LastName=lastname;
        Mailingpostalcode=Postalcode;  
        List<Contacts> contacts[select ID,Name FORM contact WHERE LastName=:lastname AND Mailingpostalcode=:Postlecode];
        return Contacts;
    }
}

But I'm unable to write an anonymous apex script to execute this. Any help?

5

The following code will solve your problem:

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

Let's view it line-by-line:

public class ContactSearch

This is just a class definition. Public says that this class is in public scope

public static List<Contact> searchForContacts(String lastName, String mailingPostalCode)

This is method declaration. Again, public keyword makes this method public accessible(public scope).
Static means that this method is class-method(static-method) and not instance-method. Difference is that for instance-method you would omit static keyword, but you would need an instance of that class to call this method.
List<Contact> is return type of the method. It means that this method will return a List of Contact objects.
searchForContacts is method name, use it to call this method.
String lastName, String mailingPostalCode are parameters that are expected by this method.

List<Contact> retList = [SELECT Id, Name, LastName, MailingPostalCode
                                 FROM Contact
                                 WHERE LastName = :lastName AND MailingPostalCode = :mailingPostalCode];

This line assigns the result of the query to retList variable.
List<Contact> retList makes a variable with name retList of type List<Contact>.
SELECT Id, Name, LastName, MailingPostalCode FROM Contact is a simple query that will return all contacts. However, you only need the ones that match given parameters, so the next line of the query is:
WHERE LastName = :lastName AND MailingPostalCode = :mailingPostalCode
This adds a filter to the query, so that it will only return results which match LastName and MailingPostalCode with given parameters.

Note the : in the query before the parameters:

If you want to use variables in your query, they should have : in front. That tells your code that it's not the value to be searched for, but a variable from the code

return retList; will return the list of matching contacts that you just found

update: alternativly, instead of creating a retList variable, you could immediately return the query. Like this:

public class ContactSearch {
        public static List<Contact> searchForContacts(String lastName, String mailingPostalCode) {
            return [SELECT Id, Name, LastName, MailingPostalCode
                                     FROM Contact
                                     WHERE LastName = :lastName AND MailingPostalCode = :mailingPostalCode];
        }   
    }
2
  • actually, instead of creating a retList variable, you could immediately return the query – Novarg Feb 10 '15 at 9:35
  • mmmmh, you're giving the challenge answer ! thought that was forbidden here – benahm Mar 30 '17 at 12:20
0
public class ContactSearch {
   public static List<Contact> searchForContacts(String lName,String pCode){
        return [SELECT ID,Name FROM Contact WHERE lastName =:lName AND MailingPostalCode =:pCode];
   }
}

You can validate this code by just clicking on the check button on your trilhead challenge page.

You can test this by writing following script in developer console anonymous block:

System.debug(ContactSearch.searchForContacts('testName','testCode'));

Let me know if you any problem in this.

Thanks

Not the answer you're looking for? Browse other questions tagged or ask your own question.