OL Learn

Stop and Exit From a loop in Run Script

I have a Run Script which loops and checks whether a certain field has a said value. I would like to stop and break out of the Run Script (JavaScript) as soon as the first value is found or as soon as my loop condition is true.

Is there any way to achieve it? Otherwise the script just sits and loops unnecessarily until it reaches the end of the result set.

Well if it’s your script itself that loops, you can break out of any Javascript loop using the break command. If, on the other hand, your script is inside a Workflow loop, you can’t break out of it but I suggest simply storing a value in a process variable or a JobInfo and creating a condition that checks for that value as the very fist step inside the loop. The loop will still go through all its iterations but it will do so very quickly because once the condition is met, it will never run the processing tasks.

1 Like

The script was inside the Loop plugin. Your suggestion worked flawlessly. Thanks.