Use For Each Loop task

This tutorial shows you how to use the For Each Loop task for string concatenation. In this tutorial, you will create two integrations; a main integration and a sub integration. The main integration loops through a String array and for each element in the array, calls the sub integration which does the concatenation. The output after running the main integration is a concatenated string.

To complete this tutorial, perform the following tasks:

Create the main integration

To create a new integration, perform the following steps:

  1. Go to the Apigee UI and sign in.
  2. Select your organization using the drop-down menu in the upper left corner of the UI.
  3. Click Develop > Integrations.
  4. Click CREATE NEW.
  5. Enter a name and description in the Create Integration dialog.
  6. Select a Region for the integration from the list of supported regions.
  7. Click Create to open the integration designer.
  8. In the integration designer, click +Add a task/trigger > Trigger > API Trigger.
  9. Drag the API trigger element to the integration designer.
  10. Click +Add a task/trigger > Tasks > For Each Loop.
  11. Drag the For Each Loop element to the integration designer.
  12. Add a connection (edge) from the API trigger element to the For Each Loop element:
    1. Click the Fork icon attached to the side of the API trigger element.
    2. While still holding the mouse pointer, drag the mouse pointer to a region inside the For Each Loop element.
    3. Release the mouse pointer.

Create variables in the main integration

Create the following variables in the main integration:

  • WordArray: A String array. The array values are sent to the sub integration for concatenation.
  • TempString: A temporary string variable.
To create the variables, perform the following steps:
  1. In the main integration, click VAR on the menubar.
  2. Click Add +.
  3. In the Create Variable dialog, set the following properties:
    • Name: Enter WordArray
    • Data Type: Select String Array
    • Default Value: Enter Hello, World, Apigee, Integrations
  4. Click Create.
  5. Click Add +.
  6. In the Create Variable dialog, set the following properties:
    • Name: Enter TempString
    • Data Type: Select String
  7. Click Create.
  8. Click OK to close the Variables dialog.

Create the sub integration

Now, create a sub integration (new integration) that concatenates the string.

To create the sub integration, perform the following steps:

  1. In the Apigee UI, click Develop > Integrations.
  2. Click CREATE NEW.
  3. Enter a name and a description for the integration, and then click Create.
  4. In the integration designer, click +Add a task/trigger > Trigger > API Trigger.
  5. Drag the API trigger element to the integration designer.
  6. Click +Add a task/trigger > Tasks > Data Mapping.
  7. Drag the Data Mapping element to the integration designer.
  8. Add a connection (edge) from the API trigger element to the Data Mapping element.

Create variables in the sub integration

Create the following variables in the sub integration:

  • SubIntegrationInput: An input variable of the integration. When the sub integration runs, this variable contains a value from the WordArray variable of the main integration.
  • SubIntegrationOutput: An output variable of the integration. When the sub integration completes a run, this variable has the concatenated string.
  • SubIntegrationPreviousValue: A input variable to hold the intermittent concatenated string for each loop. This value for this variable is passed from the main integration.
To create a variable, perform the following steps:
  1. In the main integration, click VAR on the menubar.
  2. Click Add +.
  3. In the Create Variable dialog, set the following properties:
    • Name: Enter VARIABLE_NAME
    • Data Type: Select String
  4. Specify if the variable is an input or a output variable:
    • If you are creating the SubIntegrationInput or the SubIntegrationPreviousValue variable, select Use as an input to integration.
    • If you are creating the SubIntegrationOutput variable, select Use as an output of integration.
  5. Click Create.
Repeat the steps for creating each variable in the sub integration.

Configure the sub integration

To configure the sub integration, perform the following steps:
  1. Click the Data Mapping element to view the configuration pane.
  2. Click .
  3. In the Data Mapping Editor:
    1. Drag the SubIntegrationPreviousValue variable to the Input column.
    2. In the SubIntegrationPreviousValue variable, click Add function and the select CONCAT.
    3. Drag the SubIntegrationInput variable to the CONCAT function.
    4. Drag the SubIntegrationOutput variable to the Output column.
  4. Close the Data Mapping Editor.
  5. Click Publish.

Configure and run the main integration

Now, go back to the main integration and configure it to call the sub integration.

To configure the main integration, perform the following steps:

  1. Open the main integration in the integration designer.
  2. Click the For Each Loop element to view the configuration pane of the task.
  3. Set the following properties in the configuration pane:
    • List to iterate: Select WordArray.
    • API Trigger ID: Enter trigger ID of the sub integration. You can get the trigger ID value from the sub integration's API Trigger element.
    • Integration name: Enter the name of your sub integration.
    • Iteration element sub-integration mapping: Select SubIntegrationInput.
    • Response parameter override mapping:
      • In the Sub-Integration output to map from drop-down, select SubIntegrationOutput.
      • In the Integration variable to override drop-down, select TempString.
    • Map to additional sub-integration input(s):
      • In the Integration variable to map from drop-down, select TempString.
      • In the Sub-Integration input to map to drop-down, select SubIntegrationPreviousValue.
  4. Click Publish.

If both the main and the sub integration run successfully, in the execution summary dialog, you can see the concatenated string HelloWorldApigeeIntegrations in the TempString variable.