Activity Forums Salesforce® Discussions Why can't we use DML and SOQL inside the for loop in Salesforce?

  • Piyush

    Member
    September 3, 2019 at 7:49 am

    Hi Achintya,

    By moving queries outside of for loops, your code will run faster, and is less likely to exceed governor limits. A common mistake is that queries are placed inside a for loop. There is a governor limit that enforces a maximum number of SOQL queries. When queries are placed inside a for loop, a query is executed on each iteration and governor limit is easily reached. Instead, move the SOQL query outside of the for loop and retrieve all the necessary data in a single query.

Log In to reply.

Popular Salesforce Blogs

Popular Salesforce Videos