function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Alex SidlinskiyAlex Sidlinskiy 

Compile Error: Return value must be of type: Contact

I'm getting "Return value must be of type: Contact". Not clear on how to address it. Any help much appreciated.
public class ContactSearch{
    
	//static seartchForContacts method of type Contact
    public static Contact searchForContacts(String inputlastName, String inputPostalCode) {

    	String lastNameToBeMatched = inputlastName;
    	String postalCodeToBeMatched = inputPostalCode;

        //Check whather user input varialbes empty or not
    	if(lastNameToBeMatched != null && postalCodeToBeMatched != null){
	    	//Return matched contacts in a list of type Contact
	    	List<Contact> matchedContacts = [SELECT Account.Name FROM Contact 
	                 WHERE LastName =:lastNameToBeMatched AND MailingPostalCode=:postalCodeToBeMatched];
			return matchedContacts ;//ERROR IS ON THIS LINE

		}else{

			return null;
		}
    }
}

 
Best Answer chosen by Alex Sidlinskiy
Neetu_BansalNeetu_Bansal
Hi Alex,

You are returning the list of Contact thats why error is coming, you can change the method declartion like this :
 
public class ContactSearch{
    
    //static seartchForContacts method of type Contact
    public static List<Contact> searchForContacts(String inputlastName, String inputPostalCode) {

        String lastNameToBeMatched = inputlastName;
        String postalCodeToBeMatched = inputPostalCode;

        //Check whather user input varialbes empty or not
        if(lastNameToBeMatched != null && postalCodeToBeMatched != null){
            //Return matched contacts in a list of type Contact
            List<Contact> matchedContacts = [SELECT Account.Name FROM Contact 
                     WHERE LastName =:lastNameToBeMatched AND MailingPostalCode=:postalCodeToBeMatched];
            return matchedContacts ;//ERROR IS ON THIS LINE

        }else{

            return null;
        }
    }
}

Please let me know if I can help you more.

Thanks,
Neetu

All Answers

Neetu_BansalNeetu_Bansal
Hi Alex,

You are returning the list of Contact thats why error is coming, you can change the method declartion like this :
 
public class ContactSearch{
    
    //static seartchForContacts method of type Contact
    public static List<Contact> searchForContacts(String inputlastName, String inputPostalCode) {

        String lastNameToBeMatched = inputlastName;
        String postalCodeToBeMatched = inputPostalCode;

        //Check whather user input varialbes empty or not
        if(lastNameToBeMatched != null && postalCodeToBeMatched != null){
            //Return matched contacts in a list of type Contact
            List<Contact> matchedContacts = [SELECT Account.Name FROM Contact 
                     WHERE LastName =:lastNameToBeMatched AND MailingPostalCode=:postalCodeToBeMatched];
            return matchedContacts ;//ERROR IS ON THIS LINE

        }else{

            return null;
        }
    }
}

Please let me know if I can help you more.

Thanks,
Neetu
This was selected as the best answer
Shyama B SShyama B S
Might be because you are returning a List<Contact> while your return type is just Contact. Try:
public static List<Contact> searchForContacts(String inputlastName, StringinputPostalCode)
{
}