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
RashiRashi 

Hi i am new to salesforce

How we can check for loop count in apex class?
AbhishekAbhishek (Salesforce Developers) 
Integer loopCount = 0;
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.
Malika Pathak 9Malika Pathak 9
Hi Rashi Malhotra,
Plese find the solution .How we can check for loop count in apex class?
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);
    }
}
Kindly mark it as best answer if it helps so that it can help others in the future.

Warm Regards,
Malika Pathak
 
Akshay Dhiman 63Akshay Dhiman 63
Hi Rashi,
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
RashiRashi
Thanks #Abhishek,#Malika and Akshay :)
AbhishekAbhishek (Salesforce Developers) 
Let me know if it helps you and close your query by marking it as solved so that it can help others in the future.

Thanks.