Courseiva
Performance →mediumMultiple Choice

SF-PD2 Performance Practice Question

A developer is optimizing an Apex trigger on the Opportunity object that updates related OpportunityLineItem records. The trigger currently performs a SOQL query for each OpportunityLineItem to check a field on the parent Opportunity. This causes performance degradation and governor limit errors. Which technique should the developer use to improve performance?

⚠ Common exam trap

The trap here is thinking that moving the query to a future method or adding a LIMIT solves the loop query problem, when the real fix is to bulkify by using a Map.

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 Map to store parent Opportunity records and reference them in the loop.

Using a Map to store parent Opportunity records is the correct approach because it bulkifies the code by querying all necessary Opportunities once and then referencing them in the loop. This eliminates the query inside the loop, reducing the number of SOQL queries and improving performance. The other options do not address the root cause.

Answer analysis

Option-by-option breakdown

For each option: why learners choose it and why it is or isn't the right answer here.

  • ✗

    Add a LIMIT clause to the SOQL query to reduce the number of records returned.

    Why it's wrong here

    Adding a LIMIT reduces the number of records per query but does not address the fact that the query is executed inside a loop. The total number of queries remains high, and the LIMIT could cause incorrect results if the needed record is not within the limit. It does not solve the performance issue.

  • ✗

    Move the SOQL query to a future method to run asynchronously.

    Why it's wrong here

    Moving the query to a future method does not eliminate the query inside the loop; it just shifts it to an asynchronous context. The future method would still run per record or per batch, potentially exceeding limits. It also adds complexity and may not be suitable for real-time updates.

  • ✓

    Use a Map to store parent Opportunity records and reference them in the loop.

    Why this is correct

    Using a Map to store parent Opportunity records allows the trigger to avoid querying inside a loop. The developer can query all necessary Opportunities once, populate a Map keyed by Id, and then retrieve parent records from the Map within the loop. This reduces the number of SOQL queries to one, improving performance and avoiding governor limits.

  • ✗

    Use a trigger handler framework to separate logic from the trigger.

    Why it's wrong here

    A trigger handler framework improves code organization and maintainability but does not inherently fix performance issues like queries inside loops. The underlying problem of querying per OpportunityLineItem would persist unless the code is bulkified. The framework alone is not a performance optimization.

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 →

How Courseiva writes practice questions · Editorial policy

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.