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:
I tried using an Exception Handler (AllExceptions), but I am facing issues like:
Also, I noticed the Abort Transaction property — not sure how it impacts this behavior.
Can someone guide me on:
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
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
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:
Current Behavior:
Expected Behavior:
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
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.