ForEach and Parallel ForEach

⚡For Each Scope

Configuration:

  • Collection: Expression that defines the collection.
  • Counter Variable Name: Stores the iteration count.
  • Batch Size: Splits the collection into sub-collections based on the specified value.
  • Root Message Variable Name: Stores the original payload.

Behavior:

  • Splits the payload into elements and processes them sequentially.
  • Executes in order (one by one).
  • Does not modify the payload — outside the scope, the payload remains the same as the initial input.
  • Variables declared inside the scope are accessible outside.
  • If one element fails, an exception is thrown, and For Each stops processing unless error handling is implemented.


⚡ Parallel For Each Scope

Configuration:

  • Collection: Expression that defines the collection.
  • Timeout: Maximum execution time (in ms). If exceeded, a timeout error is raised.
  • Max Concurrency: Defines the number of parallel threads to run at once.
  • Target Value: Stores the processed payload (defaults to flow’s payload).
  • Target Variable: Stores the evaluated expression result (defaults to the operation output).

Behavior:

  • Splits the collection and processes elements in parallel.
  • Onces it finishes the execution it aggregates the output and gives the final payload.
  • Execution order is not guaranteed.
  • Variables declared outside the scope remain unchanged even if modified inside.
  • Variables declared inside are not accessible outside.
  • Variables are independent to each event inside paralle foreach.
  • If an error occurs, a composite error is thrown, and the scope terminates.
  • Failed records can be handled using error handling (e.g., Try + On Error Continue).


📝 Key Takeaways

  • Use For Each when you need sequential processing and payload consistency.
  • Use Parallel For Each for faster processing of large datasets where order doesn’t matter.
  • Always implement proper error handling (Try + On Error Continue) to avoid failures stopping the flow.

📝 Interview Questions

  • You have 10,000 records. Which scope will you use and why?
  • If 1 record fails in Parallel For Each, what happens?
  • How does For Each differ from a Batch Job?
  • What is the role of the Counter Variable Name in For Each?
  • Are variables declared inside For Each accessible outside the scope?
  • Does For Each modify the payload inside the scope? What happens outside?
  • How are results aggregated after parallel execution?
  • Are variables declared inside Parallel For Each accessible outside? Why not?
  • How does error handling behave in Parallel For Each? What is a composite error?



To view or add a comment, sign in

Others also viewed

Explore content categories