1
public class ContactSearch {
    public static list<contact> searchForContacts(string lastname,string MailingPostalCode)
    { 
        list<contact>ContactList=[select id,name,lastname,mailingpostalcode from contact where lastname=:'lastname' and mailingpostalcode=:'MailingPostalcode'];
        return contactlist;
    }
}

error thrown====>

Challenge not yet complete in [email protected] Executing the 'searchForContacts' method failed. Either the method does not exist, is not static, or does not return the expected contacts.

2

1 Answer 1

1

The parameters you have passed to the method are not getting utilized in the logic of the method. In below line of code, You have passed the value as a static string in where clause of your query and not as a variable.

list<contact> ContactList=[select id,name,lastname,mailingpostalcode from contact where lastname=:'lastname' and mailingpostalcode=:'MailingPostalcode'];

Instead you need to remove the single quotes from lastname and mailingpostalcode in where clause and it will solve your problem.

list<contact> ContactList=[select id,name,lastname,mailingpostalcode from contact where lastname=:lastname and mailingpostalcode=:MailingPostalcode];

You should know about using a bind variable in soql query.

SOQL and SOSL statements in Apex can reference Apex code variables and expressions if they’re preceded by a colon (:). This use of a local code variable within a SOQL or SOSL statement is called a bind. The Apex parser first evaluates the local variable in code context before executing the SOQL or SOSL statement.

Read more about it here:- Using Apex Variables in SOQL and SOSL Queries

You must log in to answer this question.

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