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
Manish Anand 10Manish Anand 10 

Error-Loop must iterate over a collection type: Account

Hi,
I am trying simple example of SOQL, to diaplay name of all the accounts, using below statements:-
Account act = [Select name from Account];
for (List <Account> a: act)
{
system.debug('Account Name'+ a);
}
It thorws an error-Loop must iterate over a collection type: Account
What, I am missing here?
Best Answer chosen by Manish Anand 10
Head In CloudHead In Cloud
This error is because the loop must iterate over a list. You can use this code to resolve this error:
list<Account> act = [Select name from Account];
for (Account a: act)
{
system.debug('Account Name'+ a);
}

 

All Answers

Head In CloudHead In Cloud
This error is because the loop must iterate over a list. You can use this code to resolve this error:
list<Account> act = [Select name from Account];
for (Account a: act)
{
system.debug('Account Name'+ a);
}

 
This was selected as the best answer
DebadyutiDebadyuti
Can you try this  as your data need to be looped throgh list of accounts
 
//List of accounts 
List<Account> act = [Select name from Account];
//loop through list of account
for (Account a: act)
{
system.debug('Account Name'+ a);
} 

*Like my post if it helps..Happy Coding :-)
Parteek Kumar 5Parteek Kumar 5
HI Manish,

Try this one..
 
List<Account> act = [Select name from Account limit 10];
for (Account a: act)
{
system.debug('Account Name'+ a);
}

Thanks,
Parteek
Manish Anand 10Manish Anand 10
Thanks Guys for the quick reply.

Regards,
Manish