Best answer

Is there any way to exit a loop early?

  • 30 November 2022
  • 2 replies
  • 749 views

Userlevel 1

I would like to loop through a list of ID’s and for each of them:

  1. Make an GET request with the id
  2. If property “foo” is true, continue on to the next one
  3. If not, exit the zap
  4. Additionally, if all objects had “foo” as true, do an action

A simple javascript example might loop like:
 

var ids = [10,20,30,40];
for(var i = 0; i < ids.length; i++) {
var response = getDataForId(ids[i]);
if( response.foo !== true ) {
break;
}
else if ( i === (ids.length - 1)) {
performAFinalAction();
}
}

Is there any way to both conditionally continue a loop based on data from actions in the loop as well as perform an action after the loop is complete (if it does complete)? I’ve been able to do one or the other, but not both.

icon

Best answer by Troy Tessalone 30 November 2022, 04:53

View original

This post has been closed for comments. Please create a new post if you need help or have a question about this topic.

2 replies

Userlevel 7
Badge +14

Hi @mbowser 

Good question.

Please clarify: Are you trying to use the Code app or are you trying to use the Looping app?

 

FYI: The Looping app runs loops in parallel.

 

How do I stop a loop from running in subsequent action steps?

All actions after the looping step will run for each iteration of the loop. If you don’t want an action step to loop:

  • After the last step you want to loop, add a filter step.
  • Set the filter to only continue if the value loop_iteration_is_last matches the (Boolean) Is True condition.

The filter will run in every loop but will only pass in the last loop. Any actions step after the filter will run once in the last loop and use only 1 task.

 

Can I create nested loops?

Nested loops are not supported. You won’t be able to turn on a Zap with more than one Looping by Zapier Step.

 

 

Alternative way to create a looping Zap:

 

Userlevel 1

I am talking about the Looping app, so thanks for informing me it executes concurrently! I’ll try an alternative solution. While I could use the Looping app and get all then check if any are false, I want to avoid unnecessary API calls when possible.