Search code examples
salesforceapexsoql

Salesforce, select from database and add to the list of Contacts


I would like select query from SOQL, and add to the list of Contacts and return it.

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

But there is an error: "Illegal assignment from List to List" at line 4. Why please? I use API version 51.


Solution

  • I copied your code and it works like a charm? What editor you're using, maybe needs updating?

    enter image description here

    You could simplify it to just

    return [SELECT Id, Name FROM Contact WHERE LastName=:sLastName AND MailingPostalCode=:sMailingPostalCode];
    

    No point initialising a list if you're going to overwrite it in next line.