PDI · Question #63
Consider the following code snippet: Given the multi-tenant architecture of the Salesforce platform, what Is a best practice a developer should Implement and ensure successful execution of the method?
The correct answer is D. Avoid performing queries inside for loops. Given Salesforce's multi-tenant architecture and governor limits, a developer's best practice is to avoid performing SOQL queries inside for loops to prevent exceeding query limits.
Question
Consider the following code snippet:
Given the multi-tenant architecture of the Salesforce platform, what Is a best practice a developer should Implement and ensure successful execution of the method?
Exhibit
Options
- AAvoid executing queries without a limit clause.
- BAvoid returning an empty List of records.
- CAvoid using variables as query filters.
- DAvoid performing queries inside for loops.
How the community answered
(20 responses)- B5% (1)
- D95% (19)
Why each option
Given Salesforce's multi-tenant architecture and governor limits, a developer's best practice is to avoid performing SOQL queries inside `for` loops to prevent exceeding query limits.
While using `LIMIT` clauses is often a good practice to restrict result sets, the primary issue leading to governor limit exceptions is the *number* of queries, not necessarily the lack of a limit on a single query.
Returning an empty list of records is a perfectly valid and often expected outcome for queries that yield no results; it does not pose a problem for governor limits or execution.
Using variables as query filters is the standard and necessary way to make dynamic SOQL queries based on input or logic; this is not an anti-pattern or a cause of governor limit issues.
Performing SOQL queries inside `for` loops is a common anti-pattern that can quickly exhaust the governor limit for the number of SOQL queries per transaction (100 for synchronous Apex). In a multi-tenant architecture, inefficient code like this can impact platform stability and lead to runtime exceptions.
Concept tested: Apex governor limits and best practices (SOQL in loops)
Source: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_gov_limits.htm
Topics
Community Discussion
No community discussion yet for this question.
