Bubble Docs

Recursive API Workflows

This section describes what recursive Workflows are and how to set them up
Help us improve this article
This article is part of a significant update to the Bubble manual and your feedback is critical to our efforts to continuously enhance our written documentation. We would greatly appreciate if you could take a moment to let us know your thoughts on the quality of it. Thank you for your support! Give feedback on this article
API workflows can be set up to be recursive, meaning that they reschedule themselves as part of the workflow.
This is not a Bubble feature per sé but a method of combining Bubble’s backend features to unlock some powerful behaviors.
If you are coming from a programming background, you may know this as a “for” loop or a “do while” loop.
By having an API workflow reschedule itself, you can open up for:
  • Looping a workflow to perform processing on a list of things
  • Setting up workflows that automatically run at set or flexible intervals

Processing a list of things

Recursive workflows are useful to repeat a set of actions multiple times, such as when you want to apply some processing on a list of database records. The reason you may want to do this rather than use the Make changes to a list of things action is that you have a lot more control over your server resources.
Database operations spend your server's resources, and if you are trying to write to hundreds or thousands of database records simultaneously or in very short intervals things may start to slow down or even time out.
By using a recursive workflow, we can instruct Bubble to go over each item one by one and we can set up a customizable delay in-between each iteration to make sure we don't overconsume server resources.
Imagine that you have a list of items that you need to process one by one. You could use a recursive workflow to process the items.
  1. 1.
    First, set up an API Workflow with a parameter containing the data type you want to process. Make sure to check the Is list/array to tell Bubble that we are passing a list of things rather than a single record. For this example, we’ll call the parameter rentalunits and set the data type to Rental Units.
  2. 2.
    Now add the action that you want to perform. For this example we’ll simply generate a random string for the name of the rental unit. Set the Thing to change to rentalunits:first item. This way we’ll make the change on the first item in the list.
  3. 3.
    Now we’ll change the Unit name field by using the Calculate Randomstring feature
  4. 4.
    Our next step is what makes this a recursive workflow. We’ll now use the Schedule API Workflow to make the workflow run again. Three points are important to include:
    1. 1.
      First, we schedule the workflow slightly into the future. This is to avoid the looping happening so fast that we spend too much of the server capacity. We’ll schedule the next iteration at the Current date/time + seconds: 1 to tell Bubble to wait for one second in-between each cycle. If you have a complex workflow with many actions you may want to increase the waiting time.
    2. 2.
      Secondly, we need to pass the same list as was originally passed to the request, but remove the first item. This way we make sure that it goes on to work on the next item in the list in the upcoming loop. We do this by passing the parameter rentalunits:minus item:rentalunits:first item
    3. 3.
      Then, we need to add a condition that checks whether there is a next item. We do this by counting the number of items left after the first one has been removed: rentalunits:minus item:rentalunits:first item:count > 0. This condition makes sure that the workflow doesn’t loop forever but stops when the count is zero.

Running at intervals

To set up a Workflow to run at set or flexible intervals, you simply set the rescheduling action to user the current date/time + any value that you want in-between the cycles. For example, setting the schedule action to use the current date/time + months:1 will ensure that the cycle runs exactly once every month from the time it is scheduled.