--- alias: user-guide-automation-business-scenarios-building-structure-step-foreach tags: - automation - connect iot description: "This documentation outlines a step for iterating through a list of items" --- # Foreach Perform an action that will determine the next step(s) | Name | Description | Mandatory | Type | Possible Values | Default Value | | ----------- | ------------------------------------------------------ | --------- | ---------------------------------------------------------- | --------------- | ------------- | | items | List of items to iterate | N | `Any[]` | | | | itemsKey | Key that points to the list of items to iterate | N | `String` | | | | itemKeyName | Name of key to use to store the iterated value | Y | `String` | | | | subSteps | Optional steps to be executed as children of this step | N | [[user-guide-automation-business-scenarios-building-structure-step|BusinessScenarioStep]]`[]` | | `[]` | ## Remarks 1. Either `items` or `itemsKey` is to be defined. When both are defined, only `items` will be used. 2. It is expected to have an array of items to iterate. Any other type will result in an error 3. Each iteration will create a new execution using only the `subSteps` defined, starting with the very first one and moving according to the rules defined for the behavior of each step type 4. The iteration will have a clean list of answers, filling only the value being iterated using the `itemKeyName` as key in the answers 5. Currently, there is no access to the scope of the rest of the scenario 1. As a way to override this limitation, the developer can fill an array with objects with the values that require to be used within the foreach loop and access them with the `itemKeyName` property of the answers. 6. All values of the array will be iterated 7. Each iteration result will be reported in the `resultKey` entry of the scenario that contained the foreach step within an array ## Example snippets ```json { "type": "Foreach", "settings": { "items": [ "a", "b", "c" ], "itemKeyName": "item" } } ``` ```json { "type": "Foreach", "settings": { "itemsKey": "lastCalculatedResults", "itemKeyName": "item" } } ```