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
Vijay@sfdcVijay@sfdc 

Error: Compile Error: expecting right curly bracket, found 'for' at line 4 column 4

Hello All,
public class AccRatingUpdate
{
    list<account> acclist=[select id, name , rating from account];
    for(account acc: acclist)
    {
      
        
    }
}

kindly help me with the above code and its error
 
Best Answer chosen by Vijay@sfdc
Manish BhatiManish Bhati
The problem is you can't write FOR loop directly in the class better use below code :-
public class AccRatingUpdate
{

    public void AccMethod{
    list<account> acclist=[select id, name , rating from account];
    for(account acc: acclist)
    {
      
        
    }
   }
}

Mark it as answer if it solves your problem.

All Answers

Manish BhatiManish Bhati
The problem is you can't write FOR loop directly in the class better use below code :-
public class AccRatingUpdate
{

    public void AccMethod{
    list<account> acclist=[select id, name , rating from account];
    for(account acc: acclist)
    {
      
        
    }
   }
}

Mark it as answer if it solves your problem.
This was selected as the best answer
Garima SGarima S
Apex class has three components to it: [1] Attributes, [2] Methods, and [3] Constructors. Methods are the actions available to objects of your class. You define the logic inside each method.
So you need to update your code and the logic you need to write must be inside a method.

Thanks,