1

I am a newbie to apex and was working through Trailhead...and I'm having issues with the below challenge. Need help from you more experienced folks out there...

Apex class that returns contacts based on incoming parameters

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 andName fields.

The return type for 'searchForContacts' must be 'List'.

Here's my code:

public class ContactSearch {

    public static List<Contact> searchForContacts(string LastName, PostalCode) {
        LastName = LastName;
        MailingPostalCode = PostalCode;     

List<Contact> contacts = [SELECT ID, Name 
                FROM Contact WHERE LastName = :LastName 
        AND MailingPostalCode= :MailingPostalCode];
   return contacts
}

I know line 3 has an unexpected token ")" but I don't know what that means or how to correct it.

Please help.

5 Answers 5

2

As AAU already mentioned, your problem lies in parameter list:

public static List<Contact> searchForContacts(string LastName, PostalCode)

PostalCode MUST have a type, in your case it should be String.

But also, another problem could(and probably would if that's the full code) arise from these 2 lines:

LastName = LastName;
MailingPostalCode = PostalCode;

Here you just set values of LastName and PostalCode to some variables that were not defined anywhere. Instead I would recommend to delete these 2 lines and edit your query from

[SELECT ID, Name 
 FROM Contact WHERE LastName = :LastName 
 AND MailingPostalCode= :MailingPostalCode];

to

[SELECT ID, Name 
 FROM Contact WHERE LastName = :LastName 
 AND MailingPostalCode = :PostalCode];

This will save you 2 lines of code, 2 allocations and will do exact same thing.

1

There are a couple of errors in your code.

  1. The parameter PostalCode should have a data type.

public static List searchForContacts(string LastName, String PostalCode) {

  1. The List object being initialized in the SOQL query should be type cast to the Contact object.

List<Contact> contacts = [SELECT ID, Name FROM Contact WHERE LastName = :LastName AND MailingPostalCode= :MailingPostalCode];

0

I had a similar problem. Be sure to declare MailingPostalCode as a String like so:

String MailingPostalCode = PostalCode;
-1

I just compiled all the bits and pieces to put together as:

public class ContactSearch 
{
    public static List<Contact>searchForContacts (string LastName, String PostalCode) {
           LastName = LastName;
            String MailingPostalCode = PostalCode;     
            List<Contact> contacts = [SELECT ID, Name FROM Contact WHERE LastName = :LastName AND MailingPostalCode= :MailingPostalCode];
            return contacts;
    }
}
1
-1

This code worked for me.

public class ContactSearch {

 public static List<Contact> searchForContacts(string abc, string def)
  {            
   Contact[] conList = [select name,id from contact where (LastName = :abc   and MailingPostalCode=def)];  

        return conList;
    }
}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .