[SalesForce] Unexpected Token – List

I have something like the following in an Apex class:

public class Picker 
{
    public static List<Type__c> getTypes()
    {
        List<Type__c> list = [select Name from Type__c where Name = 'Person'];
        return list;
    }
}

Which gives the following error:

Error: Compile Error: unexpected token: 'List' at line 5 column 8

However, turning into the following is fine:

public class Picker
{
    public static List<Type__c> getTypes()
    {
        return [select Name from Type__c where Name = 'Person'];
    }
}

Why does the first code segment not work?

Best Answer

"List" is a reserved keyword and hence it cant be used as a variable name .You can change the name and should work

public class Picker{
   public static List<Type__c> getTypes(){
      List<Type__c> listtype= [select Name from Type__c where Name = 'Person'];
     return listtype;
  }
}
Related Topic