How is your experience with the OutSystems Community?
31
Views
5
Comments
How to continue For Each loop after exception in OutSystems Reactive?

Hi,

I have a Server Action where I am looping through a list of records using a For Each. During processing, if an exception occurs on one record, the entire all loops  stops.

My requirement is:

  • If one record fails (e.g., 3rd record), it should be skipped
  • Remaining records (4th, 5th, etc.) should continue processing

I tried using an Exception Handler (AllExceptions), but I am facing issues like:

  • Flow error: "Exception handler can't cross main flow"
  • Loop still stops when exception occurs

Also, I noticed the Abort Transaction property — not sure how it impacts this behavior.

Can someone guide me on:

  1. Correct placement of Exception Handler inside For Each
  2. Proper configuration (Abort Transaction Yes/No)
  3. Best practice to log failed records and continue processing
2021-09-06 15-09-53
Dorine Boudry
 
MVP

Hi @SreenivasuluReddy Lingala ,

to accomplish this, move all logic that is inside your loop (and that could cause the exception) into a separate action, call that new action in your loop.

Now, you can add an exception handler inside that new action.  Because you handle the exception in the new action, you are still inside your loop after the call, even if it failed.

If you set the rollback to yes there, you should follow each call of that new action by a commitTransaction.  Either at the end of it, or inside your loop before moving to the next. 

If the logic is fairly simple and does only one single database update, for example, you could set rollback to no, but I don't like that approach, if in future your logic grows and does multiple things (and so should be rollbacked on failure), it is easily overlooked by the developer maintaining this.

As for logging the failure, you can do that in the exception flow of that new action.

Dorine

2023-02-20 05-20-57
Nam Nguyen

Hi,

The main problem is server action is a single transaction. if an error occurs, transaction stops and rolls back.

My approach is instead of using server action, i would use a light process to wrap all logic inside the loop. Meaning each record will have its own transaction, if failure, the record shall rollback. 

Make sure to have Exception handler in the logic to log the failed record and for the process to stop. 

With this approach, you don't need to care about Abort Transaction property. Everything is set as default.

As for logging the failure, you can add extra logic for logging on the ExceptionHandler branch. 

Since this approach use separate transaction, the data might not be real-time. There may be a slight delay after the server action completes before the data is fully visible. If real-time consistency is a requirement, you should stick with Dorine’s approach.

Cheers,

Nam


2025-09-26 09-21-58
SreenivasuluReddy Lingala

Hi,

I have implemented a Server Action in OutSystems where I am processing a list of hundreds of records using a For Each loop.

Inside the loop, I am performing Create and Update operations for each record.

Problem: If an exception occurs while processing any one record, the entire execution stops, and the remaining records are not processed.

Requirement:

  • If an exception occurs for a specific record:
    • That record should be skipped
    • The error should be logged (optional but preferred)
    • The loop should continue processing the next records

Current Behavior:

  • Exception in one iteration → Entire transaction stops → Remaining records are not processed

Expected Behavior:

  • Exception in one iteration → Skip that record → Continue loop execution for remaining records
sample action process.png
2023-09-06 07-26-35
Sudip Pal

Hey @SreenivasuluReddy Lingala ,

I am sharing an OML which explains everything. I have followed the exact approach what Dorine said.

Have a look and let me know if you need any help.

Thanks,

Sudip

ForLoop.oml
2025-04-10 11-25-12
Diogo Reis

Hi.

All the logic inside the loop should be wrapped inside an action. In that action, you must have an exception handler.

Doing this in this way, the exception is caught inside the action, and the action will return smoothly and the loop will continue.

You you have further questions, please let me know.

Community GuidelinesBe kind and respectful, give credit to the original source of content, and search for duplicates before posting.