how to break a for-each loop if exception is found?
I currently have a collection of dates, and I added a for-each loop to go through each date and check if the status in that date matches the current status (using the if/else activity). How would I break the loop if an exception is found, i.e. the status in the date does not match the current status, and follow a different flow (exception work flow)? I don’t want it to continue to the next date in the collection if the exception is found.
we can use BREAK activity in any of the part either THEN or ELSE based on your condition where you want to stop when it enters into any of the part
or we can use CONTINUE activty along the if/else condition
we got an example as well here
I added a break activity, but it keeps going back into the foreach loop for the next item. I want to break it the loop so it doesn’t go to the next time and check. After it breaks the loop, i want it to follow a different work flow (invoke exception workfile). How would i do this?
Can you verify that it is following the correct path to the break activity? Break activity will immediately exit the for each loop it is contained within.
Perhaps put in a write line right before the break activity and say “should break after this write line” or something similar.
I added a message box after the break that say “this should break here”. When i run it, and it reaches an exception, it proceeds with that message “this should break here”, but right after that, it just goes to the next item in the collection.
That is very strange behavior. Are you using 2 nested for each loops by any chance? And i agree with @siddu509, if you could provide the .xaml where this is happening we could help diagnose
Try creating a separate brand-new .xaml that has a very simple for each loop and using a break activity.
e.g. Use a for each activity with TypeArgument of integer. For each item in Enumerable.Range(1,10)
Write line item.tostring
If item>=5 Then Break
Does it work for you doing that? If so, then there is some sort of a logic breakdown within your .xaml that will be very difficult for us to troubleshoot without seeing the .xaml itself
Assign breakLoop = false
Assign x =0
While x <= dateArray.Length And breakLoop = false
Assign d = dataArray(x)
if d = your condition then
' do whatever it is with the date ...
breakLoop=true
Assign x = x + 1
@Dave@AndyMenon I made a simple xaml to test it, and it works. However, when i apply the same logic into my actual workfile, it doesnt break the for-loop.
I should also mention that the for-loop is inside a while loop. I tried adding a condition in the while loop to end, but i dont think that is working either.
I tried it with the while loop and for loop. I think it does have something to do with while-loop. It keeps looping. I think i am not approaching this right. Would you be able to show me how to break the for-loop inside of a while-loop?
There is something in your While loop that isn’t causing it to break. If you have 2 loops, each of them must have separate variables to make them break.
After breaking the For loop, do you also want to get out of the While loop? If so, are you ensuring that condition is met before breaking the for loop?
If you haven’t already done so, you should use the debug mode in your workflow and step through it one action at a time. Be sure to be looking at each variable/argument value to ensure it is what you’re expecting. This should help narrow down why it is acting weird in your workflow. Since it’s working in a newly created simple workflow it means that it has to do with the logic created in your actual workflow