Skip to main content
Únase a nosotros en TDX, en San Francisco, o en Salesforce+ los días 5 y 6 de marzo en la conferencia para desarrolladores sobre la era de agentes de IA. Registrarse ahora.

Get Hands-On with an Iterable Variable in For Loops

Learning Objectives

After completing this unit, you’ll be able to:

  • Use an iterable variable in a for loop.
  • Implement an Iterable Interface in Apex.

Iterate Within For Loops More Easily with Iterable Variable

You can now easily iterate through lists or sets using an Iterable variable in a for loop. Instead of writing complex loops, developers can use this feature to simplify working with for loops that repeat a block of code in each item in a collection. Standardize how you loop through lists and sets to make your code more consistent.

Implement and Test an Iterable Interface in Apex

In this hands-on challenge, you create a custom Apex class that implements the Iterable. You then use this class to iterate over a collection of items within a for loop. This exercise explains how to simplify iteration in Apex using the Iterable interface.

It’s time to complete the hands-on challenge to maintain your Platform Developer I certification.

Resources

Reto práctico

+500 puntos

Prepararse

Completará esta/este unidad en su propia organización de prácticas. Haga clic en Iniciar para empezar a trabajar o haga clic en el nombre de su organización para seleccionar otro diferente.

Su reto

Implement and Test Iteration Using the Iterable Interface in Apex
In this hands-on challenge, you will create a custom Apex class that implements the Iterable. You will then use this class to iterate over a collection of items within a for loop. This exercise will help you understand how to simplify iteration in Apex using the Iterable interface.

Prework:
  • Ensure you have access to a Salesforce Developer Org or a Trailhead Playground.
  • Familiarize yourself with basic Apex programming concepts.
  • Review the Salesforce documentation on the Iterable interfaces.
    • Create a custom Apex class:
      • Name: MyIterable
        • Implements: Iterable<String>
        • Class variable: private List<String> strings
    • Create a constructor for MyIterable:
      • Argument: List<String> strings
      • Initializes the class variable strings from the constructor’s argument strings
    • Implement the iterator method:
      • Return type: Iterator<String>
      • Returns an iterator for the strings list strings.iterator();
    • Create a test class:
      • Name: MyIterableTest
      • Annotation: @IsTest
    • Add a test method to the test class:
      • Name: testIterableForLoop
      • Type: void()
        • Annotation: @IsTest
    • Steps:
      • Create a list of strings: List<String> strings
      • Values: ‘Hello’,’World’
      • Create an instance of MyIterable with the list of strings.
      • Use a for loop to iterate over the MyIterable instance.
      • Print each string using System.debug.
    • Run the test class:
      • Open the Developer Console
      • Navigate to the Test menu
      • Select New Run
      • Select the MyIterableTest class and run the test
    • Verify the Output
      • Check the debug logs to verify that the for loop iterated over the collection and printed each string.
    • Expected Output:
      • The debug logs should display the following output:
        • DEBUG | Hello
        • DEBUG | World
    Comparta sus comentarios sobre Trailhead en la Ayuda de Salesforce.

    Nos encantaría conocer su experiencia con Trailhead. Ahora puede acceder al nuevo formulario de comentarios cuando quiera desde el sitio de la Ayuda de Salesforce.

    Más información Continuar para compartir comentarios