Close Menu
SalesforceCodex
    Facebook X (Twitter) Instagram
    Trending
    • Top 10 Salesforce Flow Features of Salesforce Summer ’25
    • Unlock the Power of Vibe Coding in Salesforce
    • How to Implement Dynamic Queueable Chaining in Salesforce Apex
    • How to Implement Basic Queueable Chaining in Salesforce Apex
    • How to Suppress PMD Warnings in Salesforce Apex
    • Top 10 PMD Issues Salesforce Developers Should Focus on in Apex
    • How to Use Graph API for Outlook-Salesforce Connection
    • Enhancing Performance with File Compression in Apex
    Facebook X (Twitter) Instagram
    SalesforceCodex
    Subscribe
    Tuesday, May 20
    • Home
    • Architecture
    • Salesforce
      • News
      • Apex
      • Integration
      • Books Testimonial
    • Questions
    • Certification
      • How to Prepare for Salesforce Integration Architect Exam
      • Certification Coupons
    • Integration Posts
    • Downloads
    • About Us
      • Privacy Policy
    SalesforceCodex
    Home»Salesforce»Apex»Optimizing Loop in Apex Code

    Optimizing Loop in Apex Code

    Dhanik Lal SahniBy Dhanik Lal SahniDecember 19, 2021Updated:June 11, 20239 Comments6 Mins Read
    Facebook Twitter Pinterest LinkedIn Tumblr Email
    Optimizing Apex Loop
    Share
    Facebook Twitter LinkedIn Pinterest Email

    Loop in Salesforce Apex is a block of code that is repeated until a specified condition is met. Loops allow us to do a task over and over again. We mostly use loops to iterate and search records in the list. Loops are the primary source of CPU time consumption. If we have used nested loops then it will consume resources quickly and might throw different kinds of errors in application execution. This post will help us in optimizing loop in apex Code.

    Let us see some loop examples and their performance issues. We will also check how to resolve those issues.

    Avoid Foreach Loop

    Avoid loop iteration like the below code. Yes, it is easy to write code but it has performance issues. What is that issue?

    List<Account> accs = [SELECT Id, Name FROM Account LIMIT 10000];
    for(Account acc : accs){
                acc.Name += 'Test1';
    }

    In this foreach loop, an internal iterator is created like the below code to make a loop. You can see every time an object is created and returned to the caller before the next value retrieval.

    Iterator<Object> iter = source.iterator();
    while(iter.hasNext()) {
      Object next = iter.next();
      forLoopBody.yield(next);
    }

    Avoid for loop with size() check

    We know now what is an issue in the above foreach loop. We can create simple for loop to iterate items in a loop.

    List<Account> accs = [SELECT Id, Name FROM Account LIMIT 10000];
    for(Integer i = 0; i < accs.size(); ++i){
      accs[i].Name +=  'Test1';
    }

    This code is better than the above code in most scenarios but still, this is not the correct code to write in apex. What is issue in this code?

    This code is using size() method which will be used for calculating the number of records in the list, so every loop iteration will calculate size which will impact our application performance. If the number of records is huge then it will take a longer time and will take time to complete for loop iterations.

    So what is the correct way to write for loop to processes list?

    To make a better loop to handle the record list will be calculating and store the size in one of the variables and use that variable in for loop.

    List<Account> accs = [SELECT Id, Name FROM Account LIMIT 10000];
    Integer size= accs.size();
    for(Integer i = 0; i < size; ++i){
      accs[i].Name +=  'Test1';
    }

    This loop is better in all scenarios for performance consideration. The above two loops will also work perfectly if we are not using any DB operation like we just want to create runtime collection and do the operation on that calculation.

    Access Child Loop Collection

    Some time we need to access child records in loop but we might get QueryException in SOQL for loop with the message Aggregate query has too many rows for direct assignment, use FOR loop. This exception is sometimes thrown when accessing a large set of child records (200 or more) of a retrieved sObject inside the loop, or when getting the size of such a child collection.

    For example, the query in the following SOQL for loop retrieves child contacts for a particular account. If this account contains more than 200 child contacts, the statements in the for loop cause an exception.

    for (Account acct : [SELECT Id, Name, (SELECT Id, Name FROM Contacts) 
                        FROM Account WHERE Id IN ('<ID value>')]) { 
        List<Contact> contacts = acct.Contacts; // Causes an error
        Integer countactCount= acct.Contacts.size(); // Causes an error
    }

    To avoid getting this exception, use a for loop to iterate over the child records, as follows.

    for (Account acct : [SELECT Id, Name, (SELECT Id, Name FROM Contacts) 
                        FROM Account WHERE Id IN ('<ID value>')]) { 
        Integer contactCount=0;
        List<Contact> contacts;
        for (Contact c : acct.Contacts) {
            contacts.add(c);
            contactCount++;
        }
    }

    Optimizing Loop which can throw Heap Size Issue

    Some time we fetch records and it might return so many records which might throw heap size exceed exception like querying files records in loops or fetching all records of object which might have mroe then 20,000 records.

    List<Account> accounts=[SELECT Id, Name,IsVerified__c FROM Account];
    for (Account acct : accounts) { 
        //Some Operation 
        acct.IsVerified__c=true;
    }
    update accounts;

    In the above example, we are fetching records from the account object and updating the field of those records in the loop. This code will work perfectly when we have a few records in the object. When records are huge in number like more than 20,000 and might be lesser(based on what we are writing in a loop) also we can get Apex heap size too large error. If we query and process Attachment or ContentVersion object and use content in loop code then probably heap size can exceed in few records only. So how to handle that kind of loop in code?

    In this kind of situation, we should update records in a batch of 200 in for loop like the below code. Using this approach we will use DML for the batch of 200 records and heap size will not increase.

    for(List<Account> accountList:[SELECT Id, Name,IsVerified__c FROM Account])
    {
        for (Account acct : accountList) { 
            //Some Operation 
            acct.IsVerified__c=true;
        }
        update accounts;
    }

    How to Measure Heap Size:

    We can use Limits class for knowing heap size in apex code execution.

    • Limits.getHeapSize() – Returns the approximate amount of memory (in bytes) that has been used for the heap in the current context.
    • Limits.getLimitHeapSize() – Returns the total amount of memory (in bytes) that can be used for the heap in the current context.

    We can use the below method in code to know heap size is exceeding and based on that we can do alternate code to handle list or DML operation.

    // check the heap size at runtime
    if (Limits.getHeapSize > 275000) {
         // implement logic to reduce or handle DML 
    }

    Summary:

    Loop can throw a lot of errors while SOQL or DML operation like Heap Size or too many SOQL errors if code is not written correctly. Handle loop efficiently in code so that application will not degrade performance. We can use the Limit class to know the current Heap Size and based on that we can remove some object references or we can handle some alternate approaches.

    References:

    Optimizing Salesforce Apex Code

    Performance comparison – repeat loop vs more in one loop

    apex Asynchronous apex batch apex
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email
    Previous ArticleOptimizing Salesforce Apex Code
    Next Article Sending Wrapper object to Apex from LWC
    Dhanik Lal Sahni
    • Website
    • Facebook
    • X (Twitter)

    With over 18 years of experience in web-based application development, I specialize in Salesforce technology and its ecosystem. My journey has equipped me with expertise in a diverse range of technologies including .NET, .NET Core, MS Dynamics CRM, Azure, Oracle, and SQL Server. I am dedicated to staying at the forefront of technological advancements and continuously researching new developments in the Salesforce realm. My focus remains on leveraging technology to create innovative solutions that drive business success.

    Related Posts

    By Dhanik Lal Sahni6 Mins Read

    Top 10 Salesforce Flow Features of Salesforce Summer ’25

    May 11, 2025
    By Dhanik Lal Sahni6 Mins Read

    Unlock the Power of Vibe Coding in Salesforce

    April 30, 2025
    By Dhanik Lal Sahni5 Mins Read

    How to Implement Dynamic Queueable Chaining in Salesforce Apex

    April 21, 2025
    View 9 Comments

    9 Comments

    1. Pingback: Optimizing Salesforce Apex Code | SalesforceCodex

    2. Pingback: Optimize Apex Code by Metadata Caching | SalesforceCodex

    3. Pingback: Optimize SOQL Filter in Apex Code | SalesforceCodex

    4. Pingback: Queueable Vs Batch Apex In Salesforce

    5. Sailaja on March 22, 2023 9:15 pm

      General suggestion or best practice is to avoid for loop within a for loop but I see solutions mentioned here as using for within another for loop. Is this correct? Please suggest.

      Reply
      • Dhanik Lal Sahni on March 28, 2023 7:49 am

        Hello Sailaja,
        This is only for child records of parent SOQL. We are not using a new SOQL here.

        Thank You,
        Dhanik

        Reply
    6. Pingback: Ultimate Checklist for Efficiently Querying Large Data Sets

    7. Pingback: Integrate Google reCaptcha v3 into the Salesforce Sites

    8. Pingback: Questions for Tech Lead/Salesforce Architect Interview

    Leave A Reply Cancel Reply

    Ranked #1 SALESFORCE DEVELOPER BLOG BY SALESFORCEBEN.COM
    Featured on Top Salesforce Developer Blog By ApexHours
    Recent Posts
    • Top 10 Salesforce Flow Features of Salesforce Summer ’25
    • Unlock the Power of Vibe Coding in Salesforce
    • How to Implement Dynamic Queueable Chaining in Salesforce Apex
    • How to Implement Basic Queueable Chaining in Salesforce Apex
    • How to Suppress PMD Warnings in Salesforce Apex
    Ranked in Top Salesforce Blog by feedspot.com
    RSS Recent Stories
    • How to Connect Excel to Salesforce to Manage Your Data and Metadata February 9, 2025
    • Difference Between With Security and Without Security in Apex January 2, 2025
    • Top Reasons to Love Salesforce Trailhead: A Comprehensive Guide December 5, 2024
    • How to Utilize Apex Properties in Salesforce November 3, 2024
    • How to Choose Between SOQL and SOSL Queries July 31, 2024
    Archives
    Categories
    Tags
    apex (110) apex code best practice (8) apex rest (11) apex trigger best practices (4) architecture (22) Asynchronous apex (9) AWS (5) batch apex (9) batch processing (4) code optimization (8) code review tools (3) custom metadata types (5) design principle (9) einstein (3) flow (15) future method (4) google (6) google api (4) integration (19) integration architecture (6) lighting (8) lightning (64) lightning-combobox (5) lightning-datatable (10) lightning component (29) Lightning web component (61) lwc (50) named credential (8) news (4) optimize apex (3) optimize apex code (4) Permission set (4) Queueable (9) rest api (23) S3 Server (4) salesforce (140) salesforce apex (46) salesforce api (4) salesforce api integration (5) Salesforce Interview Question (4) salesforce news (5) salesforce question (5) solid (6) tooling api (5) Winter 20 (8)

    Get our newsletter

    Want the latest from our blog straight to your inbox? Chucks us your detail and get mail when new post is published.
    * indicates required

    Ranked #1 SALESFORCE DEVELOPER BLOG BY SALESFORCEBEN.COM
    Featured on Top Salesforce Developer Blog By ApexHours
    Recent Posts
    • Top 10 Salesforce Flow Features of Salesforce Summer ’25
    • Unlock the Power of Vibe Coding in Salesforce
    • How to Implement Dynamic Queueable Chaining in Salesforce Apex
    • How to Implement Basic Queueable Chaining in Salesforce Apex
    • How to Suppress PMD Warnings in Salesforce Apex
    Ranked in Top Salesforce Blog by feedspot.com
    RSS Recent Stories
    • How to Connect Excel to Salesforce to Manage Your Data and Metadata February 9, 2025
    • Difference Between With Security and Without Security in Apex January 2, 2025
    • Top Reasons to Love Salesforce Trailhead: A Comprehensive Guide December 5, 2024
    • How to Utilize Apex Properties in Salesforce November 3, 2024
    • How to Choose Between SOQL and SOSL Queries July 31, 2024
    Archives
    Categories
    Tags
    apex (110) apex code best practice (8) apex rest (11) apex trigger best practices (4) architecture (22) Asynchronous apex (9) AWS (5) batch apex (9) batch processing (4) code optimization (8) code review tools (3) custom metadata types (5) design principle (9) einstein (3) flow (15) future method (4) google (6) google api (4) integration (19) integration architecture (6) lighting (8) lightning (64) lightning-combobox (5) lightning-datatable (10) lightning component (29) Lightning web component (61) lwc (50) named credential (8) news (4) optimize apex (3) optimize apex code (4) Permission set (4) Queueable (9) rest api (23) S3 Server (4) salesforce (140) salesforce apex (46) salesforce api (4) salesforce api integration (5) Salesforce Interview Question (4) salesforce news (5) salesforce question (5) solid (6) tooling api (5) Winter 20 (8)

    Get our newsletter

    Want the latest from our blog straight to your inbox? Chucks us your detail and get mail when new post is published.
    * indicates required

    Facebook X (Twitter) Instagram Pinterest YouTube Tumblr LinkedIn Reddit Telegram
    © 2025 SalesforceCodex.com. Designed by Vagmine Cloud Solution.

    Type above and press Enter to search. Press Esc to cancel.

    Ad Blocker Enabled!
    Ad Blocker Enabled!
    Our website is made possible by displaying online advertisements to our visitors. Please support us by disabling your Ad Blocker.