SF-PD2 Performance Practice Question
A developer is building an Apex trigger that processes 200 Opportunity records on insert. The trigger needs to query all related OpportunityLineItem records to calculate a total. To avoid hitting the SOQL query limit, which approach should the developer use?
⚠ Common exam trap
The trap here is assuming that querying inside a loop is acceptable if the number of records is small, but governor limits apply regardless of batch size and can be exceeded even with moderate data volumes.
Answer choices
Why each option matters
Answer the question above first, then reveal the full breakdown to understand why each option is right or wrong.
Correct answer & explanation
✓
Use a single SOQL query in the trigger to retrieve all OpportunityLineItem records for all Opportunity IDs in the trigger context.
The correct approach is to bulkify the trigger by performing one SOQL query that retrieves all related OpportunityLineItem records for all Opportunities in the trigger context. This avoids the governor limit on the number of SOQL queries and ensures the trigger scales for bulk operations. Querying inside a loop or per record is inefficient and will fail with large data volumes.
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✗
Use a separate SOQL query for each Opportunity but limit the loop to 100 iterations to stay within limits.
Why it's wrong here
Even limiting to 100 queries would consume the entire SOQL limit for the transaction, leaving no room for other necessary queries. Moreover, the trigger may process more than 100 Opportunities in a batch, so this approach is not scalable and risks limit exceptions.
- ✗
Perform a SOQL query inside a loop that iterates over each Opportunity record to fetch its line items individually.
Why it's wrong here
Querying inside a loop causes a separate SOQL query for each Opportunity. With 200 records, this would attempt 200 queries, exceeding the 100 SOQL query governor limit and causing a runtime exception. This anti-pattern is known as 'SOQL in a loop' and must be avoided.
- ✗
Leverage a map of OpportunityLineItem records populated by a prior query in the trigger's initial data retrieval.
Why it's wrong here
While using a map is good for efficient lookups, the stem states the trigger needs to query line items; there is no prior query mentioned. Without an initial bulk query, the map would be empty. The developer must first retrieve the line items in a single query before building a map.
- ✓
Use a single SOQL query in the trigger to retrieve all OpportunityLineItem records for all Opportunity IDs in the trigger context.
Why this is correct
A single SOQL query that uses a WHERE clause with an IN binding on the trigger's Opportunity IDs (e.g., 'WHERE OpportunityId IN :oppIds') retrieves all necessary child records in one call. This respects the governor limit of 100 SOQL queries per transaction and is the standard bulkification pattern.
About these practice questions
One of 226 original SF-PD2 practice questions on Courseiva, each with a full explanation and wrong-answer analysis — not exam dumps or protected exam content. Learn why practice questions differ from exam dumps →
JA
Written and reviewed by Johnson Ajibi, MSc IT Security
Senior Network & Security Engineer · founder of Courseiva
Last reviewed September 2026 · checked against the official Salesforce exam blueprint
This SF-PD2 practice question is part of Courseiva's free Salesforce certification practice question bank. Courseiva provides original exam-style practice questions with explanations, topic-based practice, mock exams, readiness tracking, and study analytics to help learners prepare for the SF-PD2 exam.