For Each flow logic

  • Release version: Xanadu
  • Updated August 1, 2024
  • 2 minutes to read
  • Summarize
    Summarized using AI
    This content was generated using new OpenAI-powered functionality. Results are provided on an as is basis and are not guaranteed to be accurate or complete.

    Summary of For Each flow logic

    The For Each flow logic in ServiceNow allows you to apply one or more actions to each record within a list of records sequentially. This is useful for processing multiple records individually within a single flow, such as sending notifications or updating records. However, data pill values set inside the For Each branch are only accessible within that branch and cannot be referenced outside, returning null if attempted.

    Show full answer Show less

    Processing large record sets with For Each can be resource intensive. To improve performance, you can disable reporting by adjusting the com.snc.processflow.reporting.level system property.

    Inputs and Outputs

    • Inputs: The flow expects a list of records, typically a list of Sys IDs or Records data pills. This list can be generated using actions like Look Up Records. To control processing order, sort the records before passing them to the For Each logic.
    • Outputs: Each iteration outputs the current record being processed, allowing actions inside the loop to work with it.

    Performance and Usage Guidelines

    • Limit record count: Avoid processing more than 1,000 records at once to maintain optimal flow performance. For larger datasets, split the list and use multiple flows.
    • Flow stages: You cannot add stages inside the For Each block; stages can only be added before or after it.
    • Avoid nested loops: Nested For Each loops with many records can cause flows to exceed execution quotas, potentially stopping the flow.
    • Reporting: By default, flow execution details are stored only for the first and last iterations. To track all iterations, configure flow execution settings accordingly.

    Practical Example

    A typical use case involves sending an email for each configuration item potentially affected by a change request. The flow starts on a change request creation, looks up related configuration items, and uses For Each flow logic to send individual emails per configuration item. Execution details then show the record processed in each iteration.

    Apply one or more actions to each record in a list of records.

    The For Each flow logic applies one or more actions to a list of records. The flow applies the actions contained within the flow logic to each record in the list.
    Note:
    When you set a data pill value from inside a For each item branch of flow logic, the data pill value is only available to other actions in the same branch. Referencing a data pill value that was set inside a For each branch from outside of the flow logic branch produces a null value.

    Iterating over a large number of records can be resource intensive, especially when the For Each logic block includes complex actions for each iteration. To avoid performance issues, turn off reporting using the com.snc.process_flow.reporting.level system property. For more information, see Workflow Studio flow system properties.

    Inputs

    Items
    Data type: Records
    List of Sys ID values or Records data pill specifying the records to process in sequence. You can use a Look Up Records action to generate a list of records. For more information, see Look Up Records action.
    Note:
    If you want to process items in a particular order, you must first sort the items in this input in advance. For example, use the Order by option to sort the results of a Look Up Records action.

    Outputs

    [Table name] Record
    Data type: Record
    Current record in the loop.
    Note:
    By default, all flow loops only store execution details for the first and last iterations of a loop. To report on all iterations of a loop, create a flow execution setting record for each flow that you want to collect loop execution details. For more information about flow execution settings, see Flow execution settings.

    Send an email for each configuration item potentially affected by a change

    Configuration of the For Each flow logic using a list of Configuration Item records

    This example flow starts when a change request record is created. The flow uses a Look Up Records action to find Configuration Item records assigned to the requester of the change request. The flow uses For Each flow logic to send an email about each configuration that might be affected by the change request. The output of the Look Up Records action contains the list of records to process.

    Flow execution details of the For Each flow logic

    The flow execution details show the configuration item record used for each iteration of the loop.

    General guidelines

    Use these general guidelines with a For Each flow logic.

    Avoid adding more than 1000 items
    Avoid iterating over lists with more than 1000 records. Keep your list of records smaller to optimize flow performance. To iterate over lists with more than 1000 records, divide the list into smaller sections and use multiple flows.
    Avoid defining stages that depend on a For Each flow logic
    Flow Designer prevents you from adding stages within a For Each block. You can only add stages before or after a For Each block.
    Avoid nested For Each loops
    Avoid nested For Each loops that process many records. Nested loops may cause the flow to run until it is stopped by the flow transaction quota rule, which prevents flows from running longer than an hour. For more information about transaction quotas, see Transaction quotas.