Recursive workflows in Bubble (OR, HOW TO LOOP THROUGH A LIST)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
recursive workflows what are they how do they work i think it's helpful to start with an example right out of the gate let's just say i've got this table of users right i've got these users in my database and i want to be able to make changes to multiple of these users the ones that i select right i'm selecting all these users and i want to be able to go okay like i've selected all those users i now want to mark all of these users as active so these users here on the right hand side are obviously the ones that i am selecting so i i want to make changes to all of those users at once well a recursive workflow is the most powerful way within bubble for making changes to large lists of things what we essentially want to do is something called looping we want to loop through this list of users meaning we want to go through each of them one by one and make a change to them and to have that kind of looping logic we want to use a recursive workflow a looping function is the most common use case really for you to have what we call a recursive workflow so just before we get into how to set this up in bubble let's get into this a little bit more conceptually so here i've got in my trusty mirror board a schema let's say of what our recursive workflow looks like what it's going to do so try not to take the certain all at once let's just go through it step by step so to start with we have some kind of trigger some kind of event that's going to launch this workflow so for us that's this button being clicked right mark as active what we then do is we pass we pass here see we've got some users here we pass some users into this workflow so this is the input to our recursive workflow it's a list of users right it's it's this list here right these are the users that we want to make changes to so we pass that list in and what we essentially then do is we take one of those users at a time okay and we say make changes to that user so if we're going through that list we're essentially saying look make changes to the next using that list if we're just going to go through them from start to finish 1 through 10 however many they are right then we're just going to look and we're going to say okay who's the next user in that list let's make a change to them okay so in our case that's marking this user as active this one individual user then this is where the recursiveness aspect comes in the workflow asks itself are there any more users in this list okay so if we're going through one by one here if we're going one two three four five etc etc okay at six for example the answer that question is yes are there more users yeah well there's there's at least one more there's seven okay once we're on this guy dolly huffton are there more users yes there are we keep going through so each time that the answer to that question is yes are there more users then we start over which essentially means we come all the way back to this step we've made change to that user so he can go right now we grab the next user from this list and we make changes to them okay and then we discard them and we ask the question again are there more users if yes start over okay so here we're we're looping back on ourself or where you can think of it as we're looping through this list of users and each iteration each loop we're moving from one user down to the next user right or in your case it could be anything products invoices we're moving from one item to the next each time we loop through here and the recursiveness aspect is because the workflow is triggering itself from within the workflow right once we get to this step here start over the workflow says i want to start again okay so it triggers itself to start over that's the recursiveness aspect that's what recursiveness means the logic is self-contained okay within this formula within this expression this logic contains some component which references the formula itself okay which calls it as it's called in the wider programmer so we're calling this function over and over and over again but of course then that begs the question well wouldn't this just go on forever and ever and ever and ever and ever but that's where we have an exit condition or in other words that's why we have this are there more users question okay because at some point once we go through all these users one two three four blah blah blah we get down here to galen who's holding the fort for us as the last user in this list and once we get to galen right so galen she's this user here galen make changes okay cool thanks galen you're now active now are there more users no there's not okay galen was the last user in that list and so we come down here no and this no might just be nothing okay the workload might just end here in the sense that it won't trigger itself or we have you know in the wider programming world this might be called an exit condition we have some action that only fires once this whole looping workflow has finished we're not looping anymore we sort of come out of the loop okay you've gone round the roller coaster looping looping looping looping and now you're pulling into the bay where you have to exit and dispose of your of your spew of your vomit in the nearest garbage cans that so that's what a recursive workflow is at a a high level how do we get this set up in bubble so what i've got i've got two repeating groups here on the page this first repeating group here is just doing a search for users and in this test app that i've got going on i've got 50 users in the database okay obviously you know in in your app you'll probably have a lot more data types than this so a list of users what's happening when a user when i rather click on one of these check boxes i'm triggering a workflow i'll just reload this page in step by step mode so you can see it so what happens when i click on one of these check boxes is i'm triggering a workflow and there's a condition here let's ignore the condition for now okay i'm just going to click run next okay so cool so what's happening here when this condition is true which we'll get to that condition in a second but what's happening is this event is adding a user to a second list okay to this second repeating group okay so this repeating group here actually doesn't have a data source but it gets items from this first group pushed into it so we're essentially creating like a multi-select function so how that's working we're displaying a list of users in that second repeating group which we're calling our selected users and we're adding because the repeating group has to take a list as an input so what we're feeding it is the list of users it already has okay so repeating group selected users list of users so the list of users that's already inside of this repeating group which the first time that we do this will be zero it will be empty which we're seeing here all right plus item the parent groups user of that check box that we just clicked that user that we just clicked on over here is now being added to that list okay so if i go run there we go there's one added there's another one added there's another one added and to that condition what i have is if i click on a user that is already marked as a check box then we're doing the inverse logic so we're we're asking look does that second list of users here the selected users does it contain this user that we just clicked on okay if it does then i want you to remove that user so then we're pushing into that repeating group selected users the list of users that it already has minus that user that we just clicked on okay that's the way that we have to write these expressions in bubble so then we're removing that that user and then i have you know the inverse condition for adding a user so we're first looking to see whether that repeating group of selected users contains the user that we just clicked on and there's one event that's going to remove that user if it's already in that list and another event to add that user into that selected users list if it's not already in that list okay and then my little check box here just has some conditional behavior that's just looking to see is that is the user in the cell in that selected users list okay so this is how we're managing the multi-select functionality we have a master list of users that we're feeding from that's just pulling users from the database and then a secondary repeating group just to hold the users that we're selecting so this is a very common setup that you might have in your own app this kind of multi-select functionality okay but now how do we actually loop through this list of users and make changes to them that's that's the meat of what we're talking about here so we want to click this button right this is our trigger event okay but then what do we want to happen okay so if i go start edit workflow here there actually isn't any actions that we can choose here on the page that are going to let us do what we want to do okay we could do make changes to a list of things right and feed in of type user the repeating group selected users list of users okay but this is not good practice we very really want to use this action except on very small lists because bubble will time out but it will essentially crash if we feed too many users into this action it's not designed to hold large lists of users okay so what we want to do instead is this recursive workflow setup so how do we actually set that up well we have to set that up in something called back-end workflows which maybe you're familiar with already but to get access to them we just have to go down here to our settings api and then we have to tick enable workflow api and back-end workflows okay here we go in back-end workflows now all a back-end workflow is is a workflow that is going to run on bubble's own servers or on your app's server okay so all of these actions that we have here living on this page right like adding a list to a selected users list these are actions that are happening what we call client side they're happening using the computing power of your actual browser of your computer okay your browser is think of it like the machine that's running this code okay it has limited computing power a back-end workflow is where we're going to use the computing power of your app servers okay which are actually run by amazon you know amazon web services that you might have heard of so these massive warehouses of servers that are in essence just very powerful computers very powerful machines that have the power to run a lot of very powerful logic that your browser just isn't well set up to do okay so we use back-end workflows here because it's the only way that bubble exposes the ability for us to have a recursive piece of logic to have some workflow that's going to trigger itself that's going to reference itself from within its own set of actions so if you can imagine everything within this box here is the recursive workflow okay this is like a self-contained program a self-contained piece of logic and what it needs to operate is some inputs so you can think of it like a black box it it takes some inputs right and then it does some work inside okay common analogy for this is something like ordering pizza right the the pizza shop pizza store is going to take some input from you what pizzas do you want to order and it's going to take some payment and it might take your delivery address right and then it's going to do some work inside of itself right inside of the program it's going to do some work to make those pizzas to charge your card to deliver the pizzas so what we're creating here is a similar kind of module a piece of functionality that does something and takes in some predefined input so very similar to an api if you've worked with apis okay then we're essentially creating like our own api function here that we're calling that we're triggering from within our application and so it takes some inputs and one of those inputs that it takes as we've already discussed here is this list of users okay so this list of users is being passed into the function right into the backend workflow so to set this up we first need to define what are the inputs here what are the inputs so i'm going to click here to add a backend workflow i'm going to say new api workflow and then we give it a name so you know we can call this anything we might call this update user statuses okay and you need to omit any spaces in your name here okay you can't have any spaces exposes a public api workflow okay we're not going to get into this this would allow other services to trigger this function to trigger this workflow we don't want other services to do it we're just doing it from within our own app logic here so we can turn that off okay and then these other checkboxes as well aren't important for us what is important for us is down here add a new parameter this is where we define the inputs to this function okay so one of those inputs obviously is that list of users okay so i'm going to define here list of users and it's a list okay it's a list of users so i'm going to take that box too okay but we do need some other information to make this function as well right what i could do here right if i add an action to this workflow right so this first step here and make changes to the next user so if i was to add that action i'd go make changes to thing thing to change users okay so now i'm pulling in this list of users and now i've got to just pull out a single user okay so which user right the first one well that's gonna change just the first user every single time this workflow functions probably right so this is where we need some kind of looping functionality we need the ability to go one by one through each of these users so the way that we do that is using an index okay this is user one this is user two this is user three this is user four and that means that within our workflow here if i have that list of users then i can simply go item number and then i can pull out the user with that index okay so the fourth item in the list right or the fifth item in the list and so every time that we're triggering this that index should be incremented by one that's the way that we're going to go through each one of these users so the first time it runs that index is going to be one the second time it's going to be two three four five etc etc etc and so we obviously don't want to hard code this we want this to be something that we populate dynamically and so for us to have access to this piece of information right what index are we on what number loop are we on there's another way of looking at this how many times have we looped through this workflow what user do we want to pull out of that list this time for us to have access to that number we need to define it as a parameter okay so we're going to define a parameter here that we're going to call index which is going to be of type number okay and now we can simply say okay that what item do we want to pull out of that list the index all right and of course what thing do we want to change here we're just going to change the active to yes right that's the thing that we're actually changing here and for us to complete the picture here right we're making changes to the next user okay cool then are there more users if there are start over in bubble speak right that's simply an action to call this back-end workflow again right to trigger it over again but to have a condition where we're only going to trigger it again if indeed there are more users in that list okay so we think through how we might formulate that one way might be to say okay well how many users do we have right what's the length of that list and if the index is less than the value of that total number of users well that means that there's still users left that we haven't processed right if the index is seven but the total number of users is eight then we know that we at least need to loop through this back-end workflow one more time so let's add that logic let's trigger this back end workflow again so how we're going to do that is we're going to schedule an api workflow this is the way that we trigger back-end workflows and just note we haven't dealt with this connection yet between the the trigger button mark active and the back-end workflow we're going to get to that we're just dealing with the workflow logic itself here so we're scheduling an api workflow and now we can choose this workflow again right so this is the recursive aspect where we're triggering the workflow that we're already inside of okay so recursiveness that's what that means scheduled date we can set that to be the current date or time the users so note how we're passing in parameters here how we're passing in certain parameters to this workflow okay because we're triggering it now when we trigger it we've already told it what the inputs are and anytime that we trigger it we need to now provide those inputs so the users the list of users that we're looping through here that we're cycling through one by one that's not going to change okay that's just the same list of users that we already have that we would have got when we fire this trigger event which which we haven't done yet we're going to get to that and then the index well that's just going to be whatever the current index is plus one right so that we're cycling from one user in the list now to the second user in the list and then for this logic are there more users that's where we can simply say okay well i actually only want you to re-trigger yourself here now when the index is less than right the total count of users so i could go here okay users count okay but that means that this count expression it's going to have to be calculated it's going to have to be retrieved each new time that we're triggering this workflow so what i like to do is actually just have a parameter here called count that we just get one time store it as a number because it's not going to change right this back in workflow is receiving a list of users and that list is finite it's not going to change so we'll we'll store that as another parameter that we'll define as an input that we'll receive when this workflow starts to run and then we'll just pass that through here now so this workflow here is now basically complete we don't have this exit condition yet but let's complete the picture a little bit more first and connect this button okay so this is where this this whole cascading event is going to start right this is where we trigger the first domino to fall so to speak and then each of the dominoes after that is another user that we're making changes to but it has to start somewhere right there it has to be a trigger and so that's what this button click is going to do mark active so here what we're going to do is we're going to come down to customer events schedule api workflow so this is how we schedule back-end workflows api workflow back-end workflow it's the same thing from bubble's point of view they're they're using these terms interchangeably and here we choose which api workflow we want to trigger well we only have one excellent the scheduled date we want this to just happen now you know you might and your app have some other requirement but for our purposes we can just start this now and then now we can define what the initial values for these parameters are so for users predictably right we want to take those users that we got here in this second repeating group this repeating group selected users so i'm going to go repeating groups selected users list of users the index well we're always going to start at one no need to complicate that and then the count well now we can actually add up how many selected users there are so again repeating group selected users list of users count so now we are starting to fire this workflow on this button click and we're initializing it we're starting it with these initial values here so if we come back now to the backend workflow right we've done this part mark active we're passing in some values here so we're passing in a list of users we're passing in an index right the index is one and we're passing an account as well right so that countpount just for the sake of argument let's say it's 50. all right 50 50 users in total in that list okay i don't know why those areas are all going down there but these are the inputs now to this workflow and now it's just going to cycle through here i've obviously written this in pseudo code so to speak not in the way that we would construct these expressions in bubble but in natural language right this is what we're doing make changes to the next user and then asking are there more users and cycling through so the last part of this is that when we have looped through all these users we want to fire off a notification to the admin which you might do in several different ways for our purposes we're just going to send ourselves an email let's say okay so if you can imagine here's the looping function right it's been triggered here making changes to a user scheduling itself starting over make changes scheduling itself make changes schedule so it's going around and around and around like this until we've exhausted all the users in the list until the index is equal to the count of users okay because if you could think we're passing in 50 users here on user 49 right when the index is 49 it's still going to trigger itself the index is less than 50 if we're using 50 in this example as the count of users so then it's going to trigger itself once more but it's also going to increment the index by one so now we're at an index of 50. so now we go through make changes to the user cool okay now the index 50 is it less than the count of users which is 50 no it's not so this isn't gonna fire but we can have another event here another action rather that does fire so like i said you might have different mechanisms in your application for notifying your admins or a series of things that happen you might want to from here schedule another back-end workflow that does a number of different things to notify maybe a number of different admins all depends on your app setup but for the sake of argument right let's just send ourselves an email right recursive app example as the subject line uh sorry subject line down here that that'll do as well and in the body we could just say recursive workflow complete and if we were wanting to be fancy we could store here at what time the recursive workflow started and here we could record what time the recursive workflow ended and then we could have an indicator of how long it actually took because what we're doing here when we're scheduling this this recursive workflow is we're offloading the work to your app servers to those bubble servers that i mentioned before okay so that work is now just happening asynchronously if you will it's happening independent of what your users are doing within your application okay you've you've offloaded the work you've outsourced the work to those bubble servers okay so this is just going to tell us then when that process has finished and then of course at the moment this send email action is going to fire every single time that we loop through this workflow okay because we're going to come over here check to see whether we're going to fire it again and then we're going to send the email and then the workflow is going to end so what we actually want to do here is add a condition that's only going to fire when we're at the end of this loop right which in other words when the index is equal to the count of users in that list and looks like i've actually forgotten to put my um my domain there in my email address okay that's all sorted all right so now let's actually test this out shall we so here i am let's just grab an arbitrary number of users here not every single one but some of them okay so we've got a few users in there now if i click my active we should see this field here which is just set at the moment to take whether the user's active field is yes or no and then display active if it's yes and display inactive if it's no okay and remember right this is this is just a boolean type field a yes or no field on a user so once we click mark active we should see all these checked ones turned to active and get an email as well at the end so here we go and look at that look at that tune active let's keep an eye on our email box boom then we get an email saying that our recursive workflow is complete so that's recursiveness in a nutshell just a couple of sort of disclaimers or footnotes we're doing a pretty lightweight manipulation here to the data right the logic is pretty lightweight we're just changing one field on one object every time that this this loop occurs okay but do note that this is taking up some of your app capacity which you pay for right which if you go down to your logs and you look at capacity right you can get a view of what capacity that you're using i've just got a stupid test app here so i'm not using any capacity but this will go up when your app is doing work right when you're running workflows including these back-end workflows and so you should be careful not to put too much into this workflow you have to keep an eye on it in your own app your own needs what you can do let's say that you're doing a whole bunch of manipulations here and there's quite a lot of stuff going on maybe you've got recursive workflows nested within recursive workflows and that kind of thing to stop your app timing out which is essentially when bubble will say hey you know what you've hit your reserved capacity i'm not actually able to fulfill these requests with run out of computing power what you can actually do to mitigate that is when you reschedule this workflow then just do it you know just add in a few seconds right just say look i'm going to space these 10 seconds apart that just kind of gives the servers a little bit of breathing room so there's not an exact science to this it really depends on your own application but just keep this in mind these are the types of things that you can do to kind of spread the work more uh more evenly across the surface now another thing to keep in mind is right now we're making changes to one user at a time right one user's having a change made and then we're looping back around making changes to the next user and so on and so on and so on that takes a certain amount of time okay bubble isn't the best at doing manipulations to a lot of data in a very short amount of time okay if you do need that kind of stuff look into a service like zayno i'll leave a description down below on a really helpful video that was released recently that's talking about the pros and cons the comparisons between bubble and zeno and how they might work together in your particular use case but another way that you can speed up how fast these loops happen or rather how fast you can get through a list of objects is by actually making changes to one or two or three or four or five or some larger number of users at a time so you're essentially saying make changes to these ten users and then make changes to these ten so we're skipping further down the list rather than just going one at a time and this is something that's been demonstrated by a bubbler called andrew vernon he's posted a really helpful thread on twitter which i'll link to in the description as well on comparing how much faster these workflows can go when you use this kind of setup but in a nutshell if you wanted to do something like that in your app then i'm just going to copy this expression let's say we want to make changes to two users at a time this one's making changes to the user at the current index great but if i want to make changes then to the next item as well in the list then what i can do is i can't actually go index here plus one it's not an expression that bubble exposes to me so a little work around and so if i go arbitrary text then i can actually add in here that index plus one this arbitrary text that's essentially wrapping that index plus one as a text string so for us to actually use it here in manipulations mathematical manipulations rather we need to convert it back to a number so i'm going to use this converted to number expression okay so what this is doing now this workflow is well this action is taking the current index and this work action is taking the next index or the next user in that list so we're making changes to two users every time that we loop through this and then of course when we re-trigger this workflow it's not going to be index plus one it's going to be index plus two okay and that's going to allow us to loop through this list more times and you can of course add more of these actions as well so you know one for the third item in the list the fourth item etc etc the only thing to keep note here is that for each of these loops you're now asking the servers to do a lot more work for each loop so again you have to keep an eye on your capacity spikes here and make sure that you're not hitting capacity but you've got a few different levers that you can pull here to make these go faster so in a nutshell those are recursive workflows they become an indispensable part of your bubbling toolkit if you need to make changes to any list of things that is more than some small finite number like five items at a time or do manipulations to a number of different data objects that might have relationships to one another changing a bunch of fields on on different objects then that's the type of thing where a recursive workflow that you where you loop through those lists of objects is really going to be beneficial to you so hope that's been helpful happy bubbling [Music] [Music] you
Info
Channel: Matt Neary
Views: 16,407
Rating: undefined out of 5
Keywords: no code, no-code, app, development, app builder, bubble.io
Id: 7cNWiA_tRvU
Channel Id: undefined
Length: 34min 26sec (2066 seconds)
Published: Fri Sep 02 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.