Salesforce Platform Developer I (legacy) — Question 50
How can a developer warn users of SOQL governor limit violations in a trigger?
Answer options
- A. Use Messaging.SendEmail() to continue the transaction and send an alert to the user after the number of SOQL queries exceeds the limit.
- B. Use PageReference.setRedirect() to redirect the user to a custom Visualforce page before the number of SOQL queries exceeds the limit.
- C. Use Limits.getQueries() and display an error message before the number of SOQL queries exceeds the limit.
- D. Use ApexMessage.Message() to display an error message after the number of SOQL queries exceeds the limit.
Correct answer: C
Explanation
The correct answer, C, is accurate because it allows developers to check the current number of queries using Limits.getQueries() and proactively display a warning before reaching the limit. Options A and D are incorrect as they do not prevent the limit violation; they only notify the user after the fact. Option B is also incorrect because redirecting does not address the issue of exceeding SOQL queries in a trigger context.