Skip to main content
Added more details to the answer, adding context to the original question
Source Link
Jayant Das
  • 30.7k
  • 3
  • 42
  • 68

As Salesfoce follows a multi-tenant architecture, that necessarily implies you cannot make unlimited number of queries (even though you can for custom metadata types per documentation) or use any unlimited resources. Note that there are still some rules governing the SOQL usage for custom metadata type, e.g., the below excerpt from documentation:

SOQL queries containing long text area fields Count toward Apex governor limits.

TheAnd this is the primary reason why you should not use SOQL within for loops is to avoid governor limits. This holds true irrespective of what you are querying for. Here's the motivation for advice why you should not write SOQL inside loops (refer to this link for details).

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.

As for your question -- You should not (at least try not to avoid) have queries within loops and try to utilize the best practices which ultimately yields better performance for your application.

As Salesfoce follows a multi-tenant architecture, that necessarily implies you cannot make unlimited number of queries or use any unlimited resources.

The reason why you should not use SOQL within for loops is to avoid governor limits. This holds true irrespective of what you are querying for. Here's the motivation for advice why you should not write SOQL inside loops (refer to this link for details).

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.

As for your question -- You should not (at least try to avoid) queries within loops and try to utilize the best practices which ultimately yields better performance for your application.

As Salesfoce follows a multi-tenant architecture, that necessarily implies you cannot make unlimited number of queries (even though you can for custom metadata types per documentation) or use any unlimited resources. Note that there are still some rules governing the SOQL usage for custom metadata type, e.g., the below excerpt from documentation:

SOQL queries containing long text area fields Count toward Apex governor limits.

And this is the primary reason why you should not use SOQL within for loops is to avoid governor limits. This holds true irrespective of what you are querying for. Here's the motivation for advice why you should not write SOQL inside loops (refer to this link for details).

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.

As for your question -- You should not (at least try not to) have queries within loops and try to utilize the best practices which ultimately yields better performance for your application.

Source Link
Jayant Das
  • 30.7k
  • 3
  • 42
  • 68

As Salesfoce follows a multi-tenant architecture, that necessarily implies you cannot make unlimited number of queries or use any unlimited resources.

The reason why you should not use SOQL within for loops is to avoid governor limits. This holds true irrespective of what you are querying for. Here's the motivation for advice why you should not write SOQL inside loops (refer to this link for details).

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.

As for your question -- You should not (at least try to avoid) queries within loops and try to utilize the best practices which ultimately yields better performance for your application.