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