[4.5] - Breaking out of a loop

Posted by IramK on 10-Jul-2017 04:27

Hello,

I have a loop and I would like to exit the loop once I have found a value with a particular status in the loop. How can we do this using the LOOP_BEGIN and LOOP_END scenario?

var hasOpen = false;

{!#LOOP_BEGIN.R5707634#whjW7ZHgRyy6c7ADnW5sMA}

if({!R5707634.isOpen#value}){

  hasOpen = true;
break; // doesn't work!! } {!#LOOP_END.R5707634} return hasOpen;

Cheers.

Iram

All Replies

Posted by Srinivas Panyala on 14-Jul-2017 04:16

HI Iram,

Currently, this is not supported. Please raise an enhancement request.

You can use Server API - rbv_api.selectQuery() to get the records and use JavaScript for loop to iterate the records with the break statement.

Thanks

Srinivas

Posted by Mohammed Siraj on 18-Jul-2017 01:49

Another possible solution is to define the looping construct within a JS function. In the JS function, add a return statement on the break condition.

Eg:

function  startLoop(){

    {!#LOOP_BEGIN.R5707634#whjW7ZHgRyy6c7ADnW5sMA}

                   if({!R5707634.isOpen#value}){

                          return true; // condition to break

                   }

     {!#LOOP_END.R5707634}

     return false;

}

//invoke function

startLoop();

Note: Loop template tokens in RB are pre-processed and a copy of embedded statements within a loop is added for each loop record with record specific values. This is done before the JS engine (browser  or server-side) interprets the JS code snippet. Hence, it is not considered as an optimal approach for a large number of records esp. for employing static checks. This is more suited for creating HTML markup for each loop record and other similar use-cases. As suggested by Srinivas above, leveraging API support will be more relevant for this requirement.

This thread is closed