Illegal assignment from List to List Exception in Salesforce

Illegal assignment from List to List Exception in Salesforce

We cannot create a class with the names Account, Contact, Opportunity, etc. These are Standard object names. They are reserved keywords in Salesforce. So, you cannot use reserved keywords for your class names.

Solution:

​​​​​​​Change the Class name to resolve this issue.

Sample Code for this Exception:

public class Contact {

    public static List<Contact> contactlist(){

        List<Contact> con = new List<Contact>();
        con = [ SELECT Id, Email, Name FROM Contact LIMIT 5 ];
        return con;

    }

}

Exception:

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

Another situation when trying to update Trigger:

Sample Apex Class:

public class Account {
}

Sample Trigger that is failing with this Exception:

trigger AccountTrigger on Account( before update, after update ) {
    
    Account objAcc = trigger.new.get( 0 );
    
}

Leave a Reply