Courseiva
Performance →mediumMultiple Choice

SF-PD2 Performance Practice Question

A developer is writing a trigger on a custom object that processes up to 200 records at a time. The trigger currently performs a SOQL query inside a loop to retrieve related records for each record. This causes the trigger to hit governor limits. What is the best practice to refactor the trigger for performance?

⚠ Common exam trap

The trap here is thinking that adding a LIMIT clause or using a subquery solves the problem, when the core issue is performing a query inside a loop that must be bulkified.

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

✓

Move the SOQL query outside the loop and use a Map to correlate related records with the trigger records.

Bulkifying the SOQL query by moving it outside the loop and using a Map to correlate related records is the most efficient approach. This reduces the number of queries to one, avoiding governor limits and improving performance. Other options either do not address the root cause or introduce unnecessary complexity.

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 SOQL query with a subquery to retrieve related records for all trigger records in one query, then process the results in the loop.

    Why it's wrong here

    Using a subquery can retrieve related records in one query, but it is not always possible, especially for complex relationships. Moreover, the subquery approach may still require additional processing. The most reliable bulkification pattern is to query all related records in one query using a WHERE clause with the trigger record IDs and then map them.

  • ✗

    Use a SOQL query with a WHERE clause that filters by the trigger records' IDs, and then loop through the results to update each record.

    Why it's wrong here

    While filtering by IDs is good, if the query is still inside the loop, it remains inefficient. The key is to move the query outside the loop entirely. Looping through results to update each record individually can also lead to inefficient DML operations.

  • ✗

    Use a SOQL query for each record but add a LIMIT clause to reduce the number of rows returned.

    Why it's wrong here

    Adding a LIMIT clause does not solve the fundamental issue of performing a query inside a loop. Each query still counts against governor limits, and the trigger will still fail for large batches. Bulkification is required to avoid this.

  • ✓

    Move the SOQL query outside the loop and use a Map to correlate related records with the trigger records.

    Why this is correct

    Bulkifying the SOQL query by moving it outside the loop and using a Map to associate related records with the trigger records is the standard best practice. This reduces the number of queries to one, avoiding governor limits and improving performance.

About these practice questions

Courseiva writes every SF-PD2 question from scratch — 226 in total, each with an explanation and a wrong-answer breakdown. None are copied from real exams or dumps. 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.