Study for the Salesforce Platform Developer Exam. Study with flashcards and multiple choice questions, each question has hints and explanations. Get ready for your exam!

Practice this question and more.


A developer's code fails when inserting 10,000 Lead records due to a query inside a loop. What line of code is causing the failure?

  1. Line-01: For loop structure

  2. Line-02: PostalCode null check

  3. Line-03: SOQL query inside for loop

  4. Line-05: Updating Lead in before insert

The correct answer is: Line-03: SOQL query inside for loop

The code is failing because a SOQL query is being executed inside a for loop. In Salesforce, governor limits restrict the number of SOQL queries that can be run in a single transaction to a maximum of 100. When the developer attempts to insert 10,000 Lead records and includes a SOQL query in each iteration of the loop, the system exceeds the allowed limit of SOQL queries, leading to a governor limit exception. To avoid this situation, it is best practice to perform SOQL queries outside of loops. Instead of querying within the loop, you would typically gather all necessary data beforehand and store it in a collection such as a list or a map. This allows you to process records efficiently while remaining within Salesforce's governor limits, ensuring that the code can handle the bulk insert without failure. The other aspects mentioned—such as the for loop structure, null checks, and updating records—do not inherently violate governor limits and typically would not be the direct cause of the failure in this scenario.