0
public static List<string> searchForContacts (String l1, String p1) {  
    List<Contact> c = [select name from contact where lastname=:l1 and mailingpostalcode=:p1];   
    return c;
}

I'm getting the following error:

error: illegal conversion from list to list

2

Ther is no plain List return type. You have to explicitly set the list type as in

Public List<Contact> myMethod(){}
| improve this answer | |
2

You want to change your signature from List<String> to List<Contact> in order for that code to compile.

| improve this answer | |
  • If it did, mark it as accepted so others can benefit as well. – Sebastian Kessel Jul 18 '17 at 14:45
2

For completeness, occasionally it can be useful to generalise the type e.g.

public static List<SObject> searchForContacts (String l1, String p1) {
    if (...) { 
        return [select name from contact where lastname=:l1 and mailingpostalcode=:p1];
    } else {
        ....
    }   
}

or even:

public static List<Object> searchForContacts (String l1, String p1) {  
    if (...) { 
        return [select name from contact where lastname=:l1 and mailingpostalcode=:p1];
    } else {
        ....
    }   
}

But most of the time best to use explicit types as in the other answers here.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.