Don't have an account?
Search for an answer or ask a question of the zone or Customer Support.
You need to sign in to do that
Sign in to start searching questions
Signup for a Developer Edition
Sign in to start a discussion
public class CountingLoop{ //Name of class //Name of method public static void countForLoop(){ List<Account> accountList=new List<Account>(); accountList=[select id,Name from Account];//Query account from Your Salesforce Org integer countOfAccount=0; for(Account ac:accountList){ countOfAccount=countOfAccount+1; } system.debug('countOfAccount==> '+countOfAccount); //or integer countAccount=0; for(Integer i=0;i<accountList.size();i++){ countAccount=countAccount+1; } system.debug('countOfAccount==> '+countOfAccount); //or integer count=0; for(Integer i=0;i<10;i++){ count=count+1; } system.debug('count>> '+count); } }
for ( Object obj : listOfObjects )
{
// do stuff here...
loopCount++;
}
For further reference, you can check this,
https://developer.salesforce.com/forums/?id=906F00000008w9LIAQ
If it helps you and close your query by marking it as solved so that it can help others in the future
Thanks.
Plese find the solution .How we can check for loop count in apex class? Kindly mark it as best answer if it helps so that it can help others in the future.
Warm Regards,
Malika Pathak
There no built-in way to know which loop iteration you're on. You have to keep track of it yourself. Something like the code below:
Integer loopCount = 0;
for ( Object obj : listOfObjects )
{
// do stuff here...
loopCount++;
}
Hope this explanation will resolve your query. Mark it as the best answer if you find it helpful.
Thanks
Akshay
Thanks.