PDI · Question #134
A developer wants to mark each Account in a List<Account> as either or Inactive based on the LastModified field value being more than 90 days. Which Apex technique should the developer use?
The correct answer is D. A for loop, with an if/else statement inside. To iterate through a list of Accounts and conditionally update each based on a date comparison, an Apex for loop combined with an if/else statement is the appropriate technique.
Question
A developer wants to mark each Account in a List<Account> as either or Inactive based on the LastModified field value being more than 90 days. Which Apex technique should the developer use?
Options
- AA for loop, with a switch statement inside
- BA Switch statement, with a for loop inside
- CAn If/else statement, with a for loop inside
- DA for loop, with an if/else statement inside
How the community answered
(36 responses)- B3% (1)
- C3% (1)
- D94% (34)
Why each option
To iterate through a list of Accounts and conditionally update each based on a date comparison, an Apex `for` loop combined with an `if/else` statement is the appropriate technique.
A `switch` statement is used for multiple branches based on a single value, typically an Enum, Integer, SObject type, or String, not for complex conditional logic involving date comparisons that require an `if/else`.
The `switch` statement is not suitable for this conditional logic, and embedding a `for` loop within a `switch` statement does not make sense for this problem.
While an `if/else` statement is crucial for conditional logic, it must be enclosed within a `for` loop to process each `Account` in the `List<Account>`. An `if/else` statement on its own would only apply to a single record or condition.
A `for` loop is necessary to iterate through each `Account` record in the `List<Account>`. Inside the loop, an `if/else` statement allows the developer to check the `LastModified` field of each account and apply the 'Active' or 'Inactive' status based on the 90-day condition.
Concept tested: Iterating and conditional logic in Apex
Source: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_for_loops.htm
Topics
Community Discussion
No community discussion yet for this question.