PDI practice question 5 of 30

A developer has an integer variable called maxAttempts. The developer needs to ensure that once maxAttempts is initialized, it preserves its value for the length of the Apex transaction; while being able to share the variable's state between trigger executions. How should the developer declare maxAttempts to meet these requirements?

Full set: 201 questions
Answer options
A: Declare maxattempts as a constant using the static and final keywords.
B: Declare maxattempts as a member variable on the trigger definition.
C: Declare maxattempts as a variable on a helper class.
D: Declare maxAttempts as a private static variable on a helper class. Correct answer
Correct Answer:
D. Declare maxAttempts as a private static variable on a helper class.
Explanation

To preserve a variable's state throughout an entire Apex transaction, including multiple trigger executions, the variable must be declared as static. Static variables are associated with a class and not with a specific instance of a class. Their values are initialized when the class is first loaded and are maintained for the duration of the transaction. Placing this static variable within a helper class is a Salesforce best practice that separates trigger logic from the trigger definition, making the code more modular and reusable. The private access modifier encapsulates the state within the helper class, which is also a recommended practice.

Why Incorrect

A. Declare maxattempts as a constant using the static and final keywords. This is overly restrictive. While static final preserves state, it creates a constant that cannot be changed after initialization. The requirement can be met with a mutable static variable.
B. Declare maxattempts as a member variable on the trigger definition. Triggers do not support member variables. Variables declared in a trigger are local to a single execution and do not persist across different trigger invocations within the same transaction.
C. Declare maxattempts as a variable on a helper class. This is too vague. If it refers to a non-static (instance) variable, its state would be tied to a specific object instance, which would not be shared across trigger executions.

References

1. Apex Developer Guide, "Static and Instance Methods, Variables, and Initialization Code":

Section: "Using Static Variables"

Content: "Use static variables to store information that is shared within the context of a single transaction... Static variables are not passed from one trigger execution to another." (Note: This last part refers to separate transactions. Within a single transaction, they are shared across trigger executions, e.g., before insert and after insert). The guide clarifies, "A common use case for static variables is to prevent trigger recursion." This use case demonstrates sharing state across trigger runs in one transaction.

2. Apex Developer Guide, "Triggers", "Trigger Context Variables":

Section: "Static Variable Considerations"

Content: This section provides a canonical example of using a static boolean variable in a helper class to ensure a block of code runs only once per transaction, demonstrating how a static variable's state is preserved and shared across trigger executions.

3. Apex Developer Guide, "Triggers", "Trigger and Bulk Request Best Practices":

Section: "Logic-less Triggers"

Content: This section explicitly states the best practice: "Create helper classes to contain your business logic... Triggers can then be more concise and just delegate the execution to the appropriate helper method." This supports placing the static variable in a helper class as described in the correct answer.

Scroll to Top