+ Start a Discussion
Daniel RobertsDaniel Roberts 

Illegal conversion from List to Contact

Hey everyone, trying to get my APEX skills up. Can't seem to figure out how to debug this error. Any advice? 

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

 
Best Answer chosen by Daniel Roberts
Glyn Anderson (Slalom)Glyn Anderson (Slalom)
You're returning a list of Contacts, but the method is declard to return a single Contact.  Change the method declaration return type.
public class ContactSearch{
    public static List<Contact> searchForContacts(String lastName, String zip){
        List<Contact> conts =[SELECT Id, Name FROM Contact WHERE LastName=:lastName AND MailingPostalCode=:zip ]; 
        
        return  conts;
    }
	
}

 

All Answers

Glyn Anderson (Slalom)Glyn Anderson (Slalom)
You're returning a list of Contacts, but the method is declard to return a single Contact.  Change the method declaration return type.
public class ContactSearch{
    public static List<Contact> searchForContacts(String lastName, String zip){
        List<Contact> conts =[SELECT Id, Name FROM Contact WHERE LastName=:lastName AND MailingPostalCode=:zip ]; 
        
        return  conts;
    }
	
}

 
This was selected as the best answer
Daniel RobertsDaniel Roberts
Thank you for the help Glyn! It worked, #LevelingUp! 
Alok ThakurAlok Thakur
We are working on LWC component. facing issue List to list Error.

Illegal assignment from List<Contact> to List<Contact>

public with sharing class ContactController {
    @AuraEnabled(cacheable=true)
    //public ContactController() {
    //}
    public static List<Contact> getContactList() {
        /* return [
            SELECT Id, Name, Title, Phone, Email
            FROM Contact
            WHERE Email != null
            LIMIT 10
        ]; */
        List<Contact> conts =[ SELECT Id, Name, Phone, Email FROM Contact WHERE Email != Null];
        Return conts;
    }

please help to find solusion