nerdexam
Salesforce

PDI · Question #37

A developer must modify the following code snippet to prevent the number of SOQL queries issued from exceeding the platform governor limit. public class without sharing OpportunityService( public…

The correct answer is D. Refactor the code above to perform only one SOQL query, filtering by the Set of opportunityIds. The code requires refactoring to consolidate multiple SOQL queries within a loop into a single, more efficient query to prevent exceeding Salesforce governor limits.

Submitted by weili_xi· Apr 18, 2026Logic and Process Automation

Question

A developer must modify the following code snippet to prevent the number of SOQL queries issued from exceeding the platform governor limit. public class without sharing OpportunityService( public static List<OpportunityLineItem> getOpportunityProducts(Set<Id> opportunityIds){ List<OpportunitylineItem> oppLineItems = new List<OpportunityLineItem>(); for(Id thisOppId:

opportunityIds){ oppLineItems.addAll([Select Id FROM OpportunityLineItems WHERE OpportunityId = :thisOppId)]; } return oppLineItems; } } The above method might be called during a trigger execution via a Lightning component. Which technique should be implemented to avoid reaching the governor limit?

Options

  • AUse the System.Limits.getQueries() method to ensure the number of queries is less than 100.
  • BUse the System.Limits.getlimitQueries() method to ensure the number of queries is less than 100.
  • CRefector the code above to perform the SOQL query only if the Set of opportunityIds contains
  • DRefactor the code above to perform only one SOQL query, filtering by the Set of opportunityIds.

How the community answered

(55 responses)
  • A
    5% (3)
  • B
    7% (4)
  • C
    2% (1)
  • D
    85% (47)

Why each option

The code requires refactoring to consolidate multiple SOQL queries within a loop into a single, more efficient query to prevent exceeding Salesforce governor limits.

AUse the System.Limits.getQueries() method to ensure the number of queries is less than 100.

`System.Limits.getQueries()` only retrieves the current number of SOQL queries, it does not prevent hitting the limit or reduce the query count.

BUse the System.Limits.getlimitQueries() method to ensure the number of queries is less than 100.

`System.Limits.getlimitQueries()` retrieves the maximum allowed queries, it doesn't prevent hitting the limit or reduce the query count, and is not the correct approach for an N+1 query problem.

CRefector the code above to perform the SOQL query only if the Set of opportunityIds contains

This option is incomplete but even if completed, performing the query only if the `Set of opportunityIds` meets some condition does not address the core problem of executing multiple queries inside the loop if the set contains multiple IDs.

DRefactor the code above to perform only one SOQL query, filtering by the Set of opportunityIds.Correct

The original code performs a SOQL query inside a loop, which is an N+1 query anti-pattern and will quickly hit governor limits; refactoring to a single SOQL query using the `IN` operator with the `opportunityIds` set is the most efficient solution.

Concept tested: SOQL query optimization and governor limits (N+1 query)

Source: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_SOQL_sosl_best_practices.htm

Topics

#Governor Limits#Apex#System.Limits

Community Discussion

No community discussion yet for this question.

Full PDI Practice