+ Start a Discussion
Puja khetanPuja khetan 

i am trying to write my first apex class

trying to write apex class .
when i am creating it as new apex class ,it is throwing so many error like from is invalid 
like is invalid

same code when i am running on anonumous window ,it is running fine 

public class tes
{
 List<Account> acc = new  List<Account>();
 
acc = [SELECT id, Name from Account where Name like '%Test%' ];
for (Account acc1 : acc)
    system.debug(acc1.name);
}

some one please help
vijay kumar kvijay kumar k
Hi Puja

You have written the correct query.Try to update your class like below
public class tes
{
public void getAccountnames(){
 List<Account> acc = new  List<Account>();
acc = [SELECT id, Name from Account where Name like '%Test%' ];
for (Account acc1 : acc)
    system.debug(acc1.name);
}
}

Call this method in execute anonymous window

tes t= new tes();
t.getAccountnames();

Regards
​​​​​​​Vijay