Ask a question
My last video, I demonstrated how to create a button which keeps track of how many times the workflow is run. So each time the user clicks, it goes up. We've got a count on current user, and it will limit the user to clicking it five times. But that's rather permanent. For most SaaS apps, you'll want to be limiting an action maybe to every month. So how do we go about doing that?
Well, let me show you. I would start by creating on the user a new field, and we'll call this button clicked date. Remember, I'm just using a very simple example of this button being clicked, but you can use this with any workflow to track the number of times and to limit how many times that is run. And I'm going to make this into a list. And this is going to allow us to collect dates and timestamps of when the button is clicked. So instead of us doing a simple count, I'm going to say that this is button clicked dates, add current time and date. So let me run this and show you what difference it makes. In fact, I'll delete that for now.
Limiting workflows each month
And let's update clear to clear, not our account, from a different tutorial, but buttons clicked, clear list. Cool. So if I click the button, the workflow runs, and I click the button again so that we get a list. And now if I go into data, I'm running as this user here. And you can see that I've now got a list of the dates. And I'm not sure it's easy for me to edit these. No, but I can add new ones. Okay, I'm just wanting to be able to give you a full demonstration. But what I could do, I will demonstrate this without having to manipulate data in the database, is say that this button can only be clicked five times in one month. So let me clear out my simple count. And this time I'm going to go for current user buttons clicked dates. And I can't do a group by. That was my initial way of doing it because I'm not actually doing a do a search for. This is a list in a field. So in fact, I'll go for a filter and I'll say when this date, so that's referring to each date in the list, is greater than current time date.
Okay, I need to do it the opposite way around. So unless you have the parentheses experimental feature enabled, when you do a comparison between dates, you can front load the first date with additional manipulations, but you can't do the second date. So actually, we need to approach this the opposite way around. So rather than starting with this date, we start with current time of date. And let me just think on the moment how to get this right. Current time of date is... So I'm wanting to count how many date times stamps have occurred in this month. So I will say current time of date is less than... Okay, had to pause for a moment there. Let's try and work it out. No, I need to do current time date, rounded down to month is. So that gives me midnight at the very start of the month. And so I could actually get away with saying is less than this date. And then I think I need to account for the other side of this. My brain doesn't seem to be working well on this. I'm sure you probably got there right away with the how to get what I'm trying to do, which is this to only set dates in the current month.
Right, I got it. I've been approaching it the wrong way. So you probably could sandwich the dates, but actually you can do it much more easily with current date time, extract month. So this gives you a numerical value for the month is this date, extract, month. So that's going to return only the dates in the current month. And then I can do a count and I can say is less than five. And again, I'll make it unclickable. And I'm just going to remove the condition based on my previous video of doing a count. So when the button is clicked, I add a date, and I should only be able to click on the button until the number of dates in this month is all the way up to five. Let's test that out. And in fact, I will put a text box back in, say current user, list of dates, so that we can see them.
Okay, what have I got going on here? So it is already unclickable. I've got two dates in there. So have I got my condition wrong? Isn't clickable. Yeah, right, yes, because it's almost like a double negative. It's when it isn't clickable, and I don't want it to be clickable when the number of dates is greater than five.
So I've got that the wrong way around. There we go. I can click, click, click, click. One, two, three, four, five. That should be equal to greater than five. Let's try that again. 1, 2, 3, 4, 5. And there we go. It's now unclickable.
So as I pointed out in the previous video, you might also want to consider copying this expression and adding it to your workflow simply because making a button unclickable, with the right amount of knowledge of using the browser, inspect tools, a user might be able to find a way of making a button clickable when you've said it shouldn't be clickable. And so if you really want to protect the amount that an external API is being called, you want to add that condition to the workflow as well as to your button. So to thoroughly test it and ensure that I've got this exactly right, I'm going to add in some other dates in here from different months. So we'll go for March 23, and we'll go for February. Just posting it out around in a random ones. And then let me delete all the ones from April, so all of these four ones because that's the current month.
So we can now see that the button is clickable. And to really check that I've got this right, I'm going to take this down to two. So I have two entries into this field, one from March and one from February, but the button is still now clickable. And it should allow me... Ah, I made this exact mistake in the previous video. This needs to be the opposite way around. So only clickable when it is less than five. That's when I want the action to take place and that's why it wasn't working. Okay, let's try that again. So hopefully one last time. Right, it adds in April, adds in another April. Because there are two Aprils, the button is now unclickable, and so the workflow is not going to run. Let's go back into the database. Triple quadruple check. I've got this right. Let's remove a April entry. It's now clickable again, and I can add back in an April entry and it is now unclickable.
So there you have it. I've got that earlier video which shows you simply how to limit a user's actions using account, but that is rather fixed. As I said in this video, for most SaaS apps that you might be building with bubble, you're probably looking at a monthly credit system, and this is one way of approaching that by keeping track of the number of dates that are in a particular field for the current month.