Learn how to chain and set up loops inside of Flows inside of Directus Automate.
Speaker 0: Hi. Welcome back to another episode of Short Hops. I'm your host, Brian Gillespie. And today, I'm going to show you how to loop with flows. This topic comes up a lot inside the community.
How do we chain flows together inside Directus? But if I back up a minute, flows are a great automation tool inside Directus. You can automate simple and complex workflows using the tools we provide, but there is a little bit of a learning curve, especially when you're trying to, loop through an array of objects or array of strings and perform some type of action on those within a flow. It actually requires 2 flows and we'll cover the steps in this quick video. So what are the steps needed to actually do this?
We are going to create a first flow that collects our data that we want to loop through and perform an action on. And then we will create a second flow that actually performs those actions. And then last but not least, we to the first flow, we add an trigger flow operation and pass an array to that next flow, and that will do the looping for us. So, let's get back into the real world. A really common scenario here is I've got a list of users or subscribers that I want to loop through and send individual email notifications to or could be in app notifications.
So I've got my subscriber list here, but let's go ahead and create our first flow. We'll just call it trigger email notifications. Perfect. For our setup here, the trigger, I'm just going to run this manually, and I'm going to select the subscribers collection. And I could, require somebody to select the individual subscribers here.
I'm just going to turn that off because we don't necessarily need that and I'm going to trigger this on the collection page only because I want to notify all the subscribers at one time. We will require confirmation. Do you want to email these folks? Great. So we'll finish our setup.
And the next operation inside our flow, we're gonna get our subscribers. So I'll just go to read data. We'll get our collection. We're going to choose subscribers there. And I could filter this down using a query.
So so I could grab only active subscribers or subscribers with the first name Bryant if I wanted to. Or I could use this IDs field to pluck specific subscribers by the ID. In this case, we just want to pick all the subscribers. Sounds great. And then the next flow or the next operation inside this flow, I should say, is going to be sending our email.
So in this case, we're going to trigger another flow. And inside our payload here, I'm going to go to edit raw value. I'll wrap this in quotations. We'll use our mustache syntax from Directus, and I'm just going to use git underscore subscribers, which is the key of that last operation. Whenever a flow runs, each operation has its own key and the data returned from that operation gets appended to an object that you can access the data through that key.
Great. So now we've got our payload, but we don't have a specific flow to actually run. So let's go in and create that second flow and then we'll come back and update this. So we'll create a new flow. We'll call it send email.
Maybe we even give it a nice email icon. And as far as the trigger, this is going to be triggered by another flow. The response body here is what we would return from this flow to the first flow. Lots of flows going on. We don't necessarily need to return anything here, so I'm just going to hit finish setup.
And we will set up our email action. So we'll call this send email. The 2, if I look at my subscribers, I've got email. So the syntax here is going to be dollar sign triggered.email. Great.
Make sure you hit enter to save that. And let's just give this a subject. Here's the subject and does flow looping work? Can Brian make it work? Right.
Great. So we've got our flow here. All this does is receives data from another flow, sends an email to the person that we sent. The last piece of the puzzle here is in our trigger email notifications flow. We will go in and select that new flow that we just created and boom, everything is buttoned up.
If I go back to my subscribers content collection and trigger email notifications, bada bing bada boom, we go back to our flows. I could see if this actually ran or not. So I could see the payload there. I could see what we passed to that second flow. And here in my logs, you can see this flow ran 4 separate times, which is convenient because I have 4 separate subscribers.
So I could see here's the email that was sent. And if I happen to hop open my inbox, boom, there we could see the message. So that is how you can loop flows together to create, complex scenarios that are totally automated. This is great for sending emails or other actions that you need to loop through. Hope it was helpful.
Catch you on the next episode. Ciao.