+ Start a Discussion
Darren DoudDarren Doud 

Trailhead Challenge Question Writing SOQL Queries Unit

I am working on a Trailhead Challenge and receive this error when I execute.

User-added image

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 (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>'.

Here's my code:

User-added image

Can someone assist me? Please.

Thank you,
Darren
Best Answer chosen by Darren Doud
ShotShot
Thats because you are trying to write your code in anonymous window.
In dev console click File - New Apex Class and write there your code.

All Answers

ShotShot
Thats because you are trying to write your code in anonymous window.
In dev console click File - New Apex Class and write there your code.
This was selected as the best answer
Darren DoudDarren Doud
Agh! Your right. I didn't even notice that after I was doing it. Must have been a long day. Thanks.
Mayank Jain 52Mayank Jain 52

public class ContactSearch {
      public static List<Contact>  searchForContacts (string lastname , string Postalcode){
      List<Contact> Contacts = [Select ID,Name from Contact where LastName = :lastname AND MailingPostalCode = :Postalcode ];
        return Contacts;
              }
       }
kuldeep paliwalkuldeep paliwal
public class ContactSearch {
//its dont execute on developer console becose of static keyword try to remove static and run on developer console
    public static List<Contact> searchForContacts(String lastName, String mailingPostalCode) {
        List<Contact> conList = [SELECT Id, Name, LastName, MailingPostalCode
                                 FROM Contact
                                 WHERE LastName = :lastName AND MailingPostalCode = :mailingPostalCode];
        return conList;
    }   
}
Ruchir SinghalRuchir Singhal
Hello All,

I am getting below error

" Executing the 'searchForContacts' method failed. Either the method does not exist, is not static, or does not return the expected contacts."

Below is my code
#############################
public class ContactSearch {

    public static List<Contact> searchForContacts (String lastname, String Postalcode)
    {
        List<Contact> Contacts = [Select ID, Name from Contact where firstname = :lastname AND MailingPostalCode = :Postalcode];
        return Contacts;
        
    }
}

Can you please help me
DineshGuptaDineshGupta
Try this.,. It works..

public class ContactSearch
{
    public static List<Contact> searchForContacts(String Str1, String Str2)
    {
        List<Contact> ContactList = [select ID,Name from Contact where LastName = :Str1 and MailingPostalCode = :Str2];
        return ContactList;
    }
}
Atul Tiwari 4Atul Tiwari 4
Its working fine for me

public class ContactSearch {
    
    Public static List<Contact> searchForContacts (String Lname,String Fname )
    {
        List <Contact> con = [SELECT Id,Name FROM Contact where Lastname=:Lname and MailingPostalCode=:Fname];
        System.debug(con);
        return con;
    }
}
chetan dabaschetan dabas
you can try this one and yes, one more thing, no need to create a new apex class it will show you an error that 
"duplicate value found: <unknown> duplicates value on record with id: <unknown>" .you can simply update in the existing class
public class ContactSearch
{
    public static list<contact> searchForContacts(string a,string b)
    {
        list<contact> cntct=[select name,id from contact where lastname=:a and MailingPostalCode=:b];
        return cntct;
    }

}
Mayank BatraMayank Batra
I dont know whats wrong here. 

I used following code in the ContactSearch Apex class :
 
public class ContactSearch {
    public static List<contact> searchForContacts (String firstName, String zipCode) {        
        List<Contact> contacts = [select ID, name from Contact where firstname =:firstName and MailingPostalCode =:zipCode];
        return contacts;
    }
}

I tested this set of code with following code in anonymous window :
 

Contact[] conts = ContactSearch.searchForContacts('Jack', '27215');
System.debug(conts.size());
for(Contact cont : conts){
	system.debug('cont ' + cont.name);    
}


The debug statements work fine in the logs. They get 1 record back and print the name Jack Rogers. But the Trailhead gives following error :

Challenge not yet complete in My Trailhead Playground 1
Executing the 'searchForContacts' method failed. Either the method does not exist, is not static, or does not return the expected contacts.

"My Trailhead Playground 1" is the only trailhead playground we have. Thoughts??

Mayank BatraMayank Batra
Any 'angels' out there?
 
Kaushik MoteKaushik Mote
Hi Mayank, 
Please use LastName instead of FirstName in the query as the criteria mentioned in the assignment is for matching LastName.
This worked fine,

public class ContactSearch 
{
      public static List<Contact> searchForContacts(String LName, String MPcode)
   {
            
       List <Contact> cts =[SELECT Id, Name from Contact
                        where LastName=:LName and MailingPostalCode =:MPcode];
   
    return cts;
      }
}