Recursive or looping workflows enable automated processes to repeatedly execute a set of actions on multiple items until a defined condition is met, mimicking real-life repetitive tasks.
Mind Map
Click to expand
Click to explore the full interactive mind map • Zoom, pan, and navigate
so let's learn about looping workflows
otherwise known as recursive workflows
so as I mentioned in the previous
session about back-end workloads one of
the really cool things about them is
that they can trigger themselves so you
can have one that does an action and
then at the end of the workflow decides
it's going to start itself again and
just keeps going round and round and
round like that so doing that is called
a recursive workflow or a looping
workflow because it is effectively
making a loop that goes round and round
and that is really useful if you want to
make changes to a lot of different
things so although the whole looping
concept can seem like it's something
straight out of computer programming
which it is actually in real life you
use it all the time when you're doing
things that require a lot of repetitive
tasks so say for example you have a pile
of 50 Christmas cards that you need to
write and sign you might have a pile of
them on the left hand side of your desk
you take the first one off the top you
write it you sign it you put it in an
envelope and you put it on the right
hand side of your desk and then you go
over and you take the next one and you
do the same and each time you're taking
a card you are running a process you're
doing the same thing which is write the
card sign it put it in the envelope and
then put it on the right hand side of
the desk and you keep repeating that
process until you get to the end of the
pile and then when you've got the last
one done from the left hand pile and
you've got a nice pile on the right hand
side then you stop so looping or
recursive workflows working exactly the
same way they take a list or a pile of
things that need to be done they run the
same process on each one one by one
until they have run out of things to run
the process on and then they stop so
it's exactly the same principle and
really works very similarly to how you
would do it in real life so let's take
this example here I have 10 things in my
database which I want to make changes to
okay and if you want you can imagine
that these are Christmas cards or just
anything users tasks projects whatever
okay so the first thing that bubble will
do when you said I want to make a change
to a list of these things is that it
will number each thing in the list okay
it won't explicitly say it's doing this
but each item will have a number that
you can refer to so it won't just be a
kind of jumbled list of things it will
be a list that's in an order
then you will tell bubble the process
that you want to run on these things so
it could be I want to update one field
it could be I want to update 20 Fields
it could be I want to delete them or I
want to save them onto something else uh
it could be any one of a number of things
then bubble will take the first item in
the list or rather you will instruct
bubble start with the first item in the
list the item numbered one and run that process
process
then once you've done that we will tell
bubble to make a decision whether to
continue or whether to stop there so if
the answer is the question should I
continue is no then bubble will stop
if the answer is yes then bubble will go
back to the list but this time it won't
take item number one
we will tell it to take item number two
and then item number three and four and
so on and each time it'll go through the
process then it will ask the question
should I continue yes or no and if the
answer to that question is yes then it
will keep going through each item one at
a time
until it gets to the end
and the last item number 10 I'll go
through the process and then it will get
to the question should I continue and
this time
the answer will be no
and so it will stop the process and the
looping will have been done and all of
these items will have been updated so
just like the question here where you'll
say well have I run out of cards to fill
in when the answer is yes which means I
should stop then you will stop signing
cards in the same way bubble will do the
same so how does it know how does it how
do we formulate this question so that it
knows when to keep going and when to
stop well all we do is that while we are
sending through this Loop we are also
sending through two other pieces of
information one of those a piece of
information is the total number of
things so we know there are 10 things in
this list so we tell but well there are
10 things in the list and then we also
tell it what is the number of the
current thing you're working on so one
two three four five six seven eight nine
or ten so anytime it is number one to
nine we will allow it we will say okay
you can continue as long as the the
current thing you're working on is not
item number 10 in
when the current thing you're working on
is item number 10 that is the last thing
in the list and therefore once you've
done the process you should stop
so although this might seem quite
abstract this is almost exactly how
looping workflows work and I will show
you now in some real examples so on this
page we have a imaginary project and we
have a button here to create tasks and
we have a button here to update tasks so
the first one I want to show you how you
can use a recursive workflow to create a
number of things in in the database and
then we're going to see how you can
update them so all I've done is I've
made a little interface here where you
can specify the number of tasks that you
want to create and then press create but
I haven't done any of the workflows yet
because we are going to do that together so
so
first let's go to our backend workplace
and we're going to make a new API
workflow which is going to be create tasks
tasks
and we are going to untick that and now
we're going to Define some parameters
some information that we want to send
through to this backend workflow so the
first thing is going to be
total tasks which is going to be a
number so this is going to be the total
number of tasks that we want created and
it's going to be that number that we
type into the box before we press create
then we're going to have something
that's called current task so that
is also a number and that is just the
number of the tasks that is currently
working on and we're going to change
that number each time it Loops through
so that goes through every task and the
third thing is going to be project
because each of these tasks is going to
be attached to a certain project the
project that we've we made up on the
page because we want to be able to see
all the tasks associated with that
project so these are all the parameters
that we need now we're going to close
that and we are going to create
a new thing and the type of thing we're
going to create is a task
and the fields we're going to set so
we're going to set project equals
and it's going to be one of our
parameters project so it's being passed
through from the page when we trigger
this workflow and then we're going to
let's set the name equals task
task
and then
let's put in a dynamic number so let's
put in the current task number so it's
going to say task and then when it's the
first task it'll be one or the second
task will be two and so on so it's going
to be name equals task and then a number
and that's all the fields we want to set
then we are going to schedule
this workflow to run again because the
first time it goes through it's going to
do it on the first task but we wanted to
do a number of tasks that we are going
to specify so we're going to to schedule
itself at the current date and time
and we're going to tell it total tasks
that's going to remain constant so if we
say we want 20 tasks that number will
remain the same we still want 20 tasks
but the current task is going to be
plus one so this is this is a really
important part because this means that
the next time it runs
this API workload this back-end workflow
that current task number has gone up by
one so the current task is going to be
called task
two for example if the first one was one
and then three and so on
and then the project that is going to
remain the same so the only thing that's
going to change and this is uh pretty
much the same with most recursive
workflows is that the there is a number
that changes each time so that it knows
that it's working through these tasks
and then very importantly we want this
workflow to stop at a certain point
because if we just trigger this workflow
now it's just going to keep going and
going and going and we haven't actually
told it to stop when it reaches the
number of total tasks so it's just going
to keep going and going going it's not
even paying any attention to this number
at all and it will be effectively in an
infinite Loop which is just going to
quickly fill up your database and cost
you loads of workload units so we
definitely need to make sure this stops
and the way we do that this is the
question where we where it asks should I
continue or not so this is where we
specify that as in this only when here
so when it's scheduling itself we're
going to put in a condition here so we
only want it to run again when the total
number of tasks
so that's a for example 20 is bigger than
than
the current task number
so if you imagine total number of task
is 20
. so when current task is one it's going
to get to this question it's going to
say oh 20 is bigger than one so I'm
going to I'm going to run I'm going to
schedule this then when it gets to 19
it's still going to say 20 is bigger
than 19 so that's okay but as it
schedules itself again it will add 1 to
19 and it will get 20. so the next time
it round it goes create tasks create a
new task task 20 it will get to here and
it'll say total tasks
is 20. current task is 20. so total
tasks is equal to current task is not
bigger than current task therefore I
should stop therefore I should not schedule
schedule
myself again so at that point it will
stop having created 20 tasks in the
database so this bit here this
conditional is what prevents it from
running infinitely and this is where it
asks the question should I continue in
the same way as if you're preparing your
Christmas cards you would look to see if
there are any left in the pile and if
there are none left in the pile then you
know you've completed your Christmas cards
cards so
so
going back to our page
so when the button create tasks is
pressed it shows pop-up create and then
when this button
is pressed
we are going to schedule
an API workflow
and it's going to be create tasks and
schedule date is going to be current
date and time
and the total tasks is going to be input
number of tasks value
the current task we always start with one
one
so it's always going to start with the
first one and then the project is going
to be the current page project
so that's going to send through the
number one to start with and then every
time it Loops through that it's going to
go two three four five six seven until
it gets to whatever number we type into
create tasks so let's do 20 tasks and
click create
and I forgot to hide the pop-up as you
can see they are being created in the
back end through this workflow and they
are one by one appearing on the page or
not one by one but
in groups so until we get to 20
and then hopefully it should stop there
because if it doesn't then we've made a mistake
mistake
or I've made a mistake okay so there it
has created all 20 tasks and it has
stopped which is exactly what we wanted
it to be
so the second example is updating tasks
so say we want to update all 20 of those
tasks how would we do that
so let's go to our backend workplace and
let's create a new backend workflow
which we will call update tasks
and again we will untick that and this
time we are going to have one more
parameter which is going to be
the list of tasks because when we did
the create tasks there was no list of
tasks to send it we were asking it to
create the tasks at that time but this
time when we're updating tasks we have
to tell it what tasks do we want it to
update so we are going to send it a list
of tasks so I have to tick this box here
to tell it that it should be expecting a
list here
we're also going to send through a value
for the total tasks which is going to be
a number
now this is optional
um you cannot you can actually uh just
ask it to count how many are in the list
of tasks every time but that just
requires that bubble does this count
every single time it Loops through the
workflow and because this number is not
going to change once once we sent this
list through that number will remain
constant we may as well just count it
once and then send it through as a
number so that's just a preferred way of
doing things we also want to send
through a number that represents the
current task we're working on so in this
workflow we call that current task
more commonly I would call it something
like index or iterator so index is
probably a simpler word but usually I
will always just call it index because
it's just simpler for me to remember so
index is the number of the tasks that we
are currently working on and that's the
number that we're going to increment by
one each time it Loops through so now
we've got those three things the list
the total number and the index then we
can but the first action in which is
going to be make changes to a thing
rather than create things we're making
changes and the thing we want to change
so we are only going to change one of
these tasks at a time the thing we're
going to change is tasks so
it's showing me tasks because this is
one of our parameters the only other
options a current user into a search for
but we want to change the list of tasks
but we can only change one thing at a
time so it says you're trying to change
a list of tasks you probably want to
change the first item of this list okay
so it is correct but we don't want to
say tasks first item because that means
every time it leaps through it's just
going to change the first item we
actually want to change item number
and then it's expecting a number here
and the number we're going to put here
is index because this is the number
that's going to change so the first time
it's going to say tasks list item number
one then the next time it will be the
tasks list item number two and so on so
that's how we are going to iterate
through each of the tasks in the list
changing them one by one and then the
thing we're going to change I think is
we are just going to change the name to task
task
and then I'm going to put in the
index number
and then I'm going to say up updated
updated
okay so task one that will change the
name to task one dash updated and task
two will be task 2 updated okay and that
will just show us that we have updated
those things and then we are going to
for the next step schedule an API
workflow and that workflow is going to
be update tasks and we're going to
schedule it for the current date and
time and the tasks list is going to
remain exactly the same
total tasks is going to remain exactly
the same
and there so we know that number is
going to be 20 in our case so it will be
20 every time but the index is going to be
be
Plus 1. so each time it's going to add
one to the index until we get to the end
and then very important we need to put
in our conditional to make sure it stops
at the right time so stop so we want it
to continue only when total tasks is
bigger than index and then when it gets
to the point where total tasks is not
bigger than index I.E total task equals
index then it will stop scheduling this
and it will stop doing this update
so now we've done that we can go back to page
page
and we can find the pop-up which is
update and I actually I was planning on
having a kind of dynamic field that we
could type in there and send that
information through but I'm going to
delete that in fact I'm going to delete
this whole pop-up because I think that
slightly over complicates things and in
most cases you won't need to do stuff
like that so update tasks
I'm just going to edit this workflow and
we are going to schedule
an API workflow and that's going to be
update tasks and it's going to be
current date and time
tasks so this is the important bit this
is where we tell it the list of tasks
that we want it to do this thing on so
in our case we are going to say
we could actually say repeating group
tasks list of tasks or we could say do a
tasks where the
the project
project equals
equals
current page project so
that will have the exact same result
because repeating group tasks is that's
how it's getting its list of tasks by
doing this exact search so it doesn't
really matter more commonly probably you
would do a search for tasks like this
but in our case we could have just
referred to the repeating Group total
tasks is going to be if I copy that expression
expression
it's going to be search for tasks count
count
so that will return 20 in our in our
situation and then index is always one
to start with because we want it to
start on number one
uh and it's worth pointing out that in
some situations you may not want it to
make changes to all the tasks in the
database so you can put as many
constraints as you want in here you can
have users selecting a certain number of
tasks or things from a list the list you
Center in here is entirely up to you and
depends on your needs in our case we are
sending through every task that is
linked to the current page project but
you could equally restrict it to only
you know half of those tasks if you only
want to update half of them
so once we have filled this in
um that's all we need to do
so you can see how it's going through
looping over and over and updating all
of them just to change the name like that
that
and then it has hopefully stopped at the
end and I'm pretty sure it has you can
see the kind of length of time that it
takes to do it and the way it can leap
through which is just a really handy
thing to do so
um I often use Loops like this to fix
data in my database so often when I've
got an app that I've been developing for
quite a while I'll realize that I want
to make a change to the database I want
to add a new field onto users for
example so for example say I had an app
that was completely free and then I
decided that I wanted to make it a paid
app then I might want to leap through
all the users in the database and you
know give them a free trial end period
or give them a flag that said a free
user or a flag that says like you know
Legacy user so anytime where I want to
make changes to all the things in the
database I use recursive workflows like
this to leap through likewise I have
apps where I need to once a day I want
to Loop through every user in the
database and say update a value on their
on their user or update a figure on
their dashboard and do calculations and
stuff so I will have a workflow that
once a day will schedule a recursive
workflow to Loop through every user or
every project or everything and update a
field on it so that the next time the
user logs in they will see this figure
has been updated so recursive workflows
are really really useful for that kind
of thing for doing updates to large
amounts of data there is also an option
to make changes to things on a list
which I'm going to do a session on next
but in general recursive workflows are a
better way of doing things because you
can have absolute visibility over
what is being updated and when so for
example we could have in our back-end
workflows we could have
in this workflow had
an email
which sent
to well
to whoever
that said update complete and we could
it only sends that email when
total tasks is index so we know that
this will only run at the point where
this is no longer true when it gets to
task 20 in our case then this will not
run and this will run so on the final
time it'll run and it'll send us an
email saying update complete and then we
know that that update has been done so
if you've got an update that takes an
hour to run because you've got thousands
of things in your database then it could
be really useful to receive that email
to know that it has completed so
recursive workflows are very useful in
that sense and we could also have it so
that once this is completed we could
schedule another recursive workflow to
do other things and because we are able
to tell when this is completed we will
then know when we can schedule this
other workflow to start and so on so you
can daisy chain a load of workflows
together uh all waiting until the formal
one has completed whereas if you use the
other action which is a schedule an API
workflow on a list you have no way of
knowing when that has finished so it's
definitely not as easy to control and
easy to manage as a recursive workflow
on the flip side now that bubble has a
workload units as part of its pricing
um it's definitely worth being very
careful when you're making your
recursive workflows because if you had
one for example that turned out to be an
infinite Loop because you'd forgotten to
put this in there uh and you and you
failed to stop it from running
um then you could be in trouble because
it's effectively going to use an
infinite amount of workload units now
bubble has said they're not going to
penalize people who have made a by
mistake made an infinite Loop and used
uploads of workload units by that so I
don't think you really need to be
worried about that but in general you
might find that your app has some
recursive workflows which are really
degrading the efficiency of the app so
you just have to be a bit careful about
when you're using this recursive
functionality and the last thing that's
worth pointing out is if you do make an
infinite Loop you can always if you go
into your logs and go to your scheduler
and click show
so there's nothing here now but this
will show you all this all the workflows
that are scheduled after a current time
so this is current date and time but you
can put this at any time in the future
and it will show them and you can click
cancel or you can click cancel all or
you can pause them so if you do have an
infinite Loop running you do have a way
to cancel them to cancel it or to stop
it from running so just be aware of that
especially while you're learning because
you may well set off an infinite Loop
but don't be too worried because you
have this way of stopping it here so
that concludes the session on recursive
workflows I hope it has been interesting
these are definitely something that you
are going to need to use at some point
so worth learning I know that the the
whole thing about that the index and the
total can be a bit confusing but once
you practice with them a few times
you'll get the hang of it and it'll
Click on any text or timestamp to jump to that moment in the video
Share:
Most transcripts ready in under 5 seconds
One-Click Copy125+ LanguagesSearch ContentJump to Timestamps
Paste YouTube URL
Enter any YouTube video link to get the full transcript
Transcript Extraction Form
Most transcripts ready in under 5 seconds
Get Our Chrome Extension
Get transcripts instantly without leaving YouTube. Install our Chrome extension for one-click access to any video's transcript directly on the watch page.