(Salesforce Apex 2024 Release) For loops now directly support Iterable

As per the Winter `24 release notes It will work directly on Iterable classes, so you don't need to call .iterator() explicitly yourself in the for loop.

The code that we currently need to write in order to iterate over an iterable is shown below. and we are relying on the more verbose while loop currently imposed on us.

Current Implementation

Iterator<Integer> iter = someClass.iterator();
while(iter.hasNext()) {
  Integer value = iter.next();
  // Do something with value
}
New Implementation as per winter `24 release notes

for(Integer value: someClass.iterator()) {
    // Do something with value
}

We could use this in almost every class we develop, including triggers, the majority of Visualforce pages, and pretty much any other place we have to write loops. It facilitates reading the code, particularly when we want to iterate over several levels of data. With Lists, Sets, and queries.


Comments

Popular posts from this blog

Understanding the Salesforce Approval Process: A Step-by-Step Guide

How to Give Permissions to Automated Process Users or System Context Users

Salesforce Custom Settings: Optimizing Your CRM for Maximum Efficiency