This course is currently a work in progress. Please join the waitlist to receive regular updates.

For loop based on a collection

In Apex, for loops can be used to go through records in a collection, one at a time. Collections can be either a list or a set. Following is an example of a for loop that runs based on a list collection:

Loading...

In the above example:

  • The list variable allContacts is initialized from a SOQL query that will contain contact records.
  • The for loop is used to iterate through the contacts list.
  • The variable contactRec is used to access individual contact records inside for loop.

Try It Yourself

Loading...