Ask a question
I've got a repeating group here and I've got a list of contacts, and I've got a first name field and a last name field. So if I was to update this first name, so to Alfred, you can see that my label here updates. And that's because my label is simply two dynamic values, my current sales contacts first name and my current sales contacts last name. But you might have a good reason in your app why you'd want to have the full name in your database. And you could do that through a workflow when you modify it. But database triggers basically mean that you've got one less thing to worry about, one less chance that you'll miss keeping data up to date and in sync with one another.
How to use database triggers with Bubble
So let me demonstrate. To get to database triggers, you need to be on a paid Bubble plan and a backend workflow is enabled, and we can go in and general, new database, trigger event. And so let's say this is update full name and contacts. So what we're going to do is make changes to a thing. And database triggers give you access to the data before and after this trigger occurs. So we're going to make changes to the one after, and we're going to change the field of... I'll just put in full name. And full name is going to be the contact after we've updated either the first name or the last name. Now, you could use this contact or you could use contacts now. It doesn't make any difference. And we'll say first name.
And we will also trim this to remove any additional spaces that the user might leave after the name. Otherwise, we'd end up with a double space in the middle. We click there, we add a space of our own and we go contacts now, last name, and we also trimmed that.
Now, with regards to workload units, we want to optimize this. And at the moment, this database trigger will run every single time anything changes with a contact. And I can limit that down. So I could say only when the contact before change, first name, is not, contact now, first name, or contact before, last name is not contact now, last name.
So my contact could have 20 other fields. It could have email address, it could have option set fields in there. But I don't want to update full name every single time one of those fields changes. I'm limiting it now just to when either first name has changed or last name has changed.
So let me update the labels in my app. So to instead say, tags, full name and refresh that. Okay, so at the moment, they're empty because the database trigger has not been run. So maybe you'll want to consider saving it at some point. Depending on when your database trigger runs, you might need to populate the full name value first. But you'll see that if I update this back to Bruce and click out, my full name is populated there as Bruce Wayne. And I can do the same for Tony, so I could put in pepper. Okay, and the field is updated.
A couple of things to say about database triggers that's really worth being aware of. I'll go back to back end. So database triggers run in the back end because they're a back end workflow. In my experience, when you run a lot of database triggers at the same time, say you've imported a CSV file with 100 entries and you're running a data space trigger on each of those entries. In my experience, it can be a little bit iffy, it can fail. There's definitely an element of Bubble queuing the data base triggers. It doesn't happen instantaneous on the front end. So just be aware of data base triggers when you're handling large amounts of data and you're pushing through, say, hundreds of entries in one go that all have a change like this going on. I just be aware of that.
The other thing to mention with database triggers is that they can't trigger each other. So I couldn't now add in a database trigger that says when full name changes, do this action because the full name change occurs in the database trigger. Having said that, that would run if you updated full name in a workflow elsewhere in your app. What I mean is that the change to full name here is not going to trigger a database trigger that is looking for changes on the full name field. So there you have it. That is a summary and an illustration of one helpful thing that you can do and make use of database triggers. If you've got any questions about database triggers, do leave a comment.
If you've got any requests for further videos, leave a comment or so. We look and try and respond to every single one.