So I've got a very simple table going on here. It's a table of a data type called vehicles, and my vehicles are made up of two option sets. I've got make and I've got model. And if we have a look at my database data for vehicles, you can see here that there's just those two fields. If I go on to the design tab, I've got the search and autocorrect, also known as the Fuzzy Search plugin by Zeroqode added to the page. And I've linked up to the input box ID of this text input here, as you can see down here. If you need a bigger, longer explanation of how to use Fuzzy Search, do check out our previous videos. We cover it in a lot more detail there.
Fuzzy Search only works with text
But what I wanted to highlight now is that I can't get the Fuzzy Search plugin here to pick up either of my option set fields because they are a data type of option set of the option sets I've created, make and model. Whereas Fuzzy Search needs to be the... Well, the search in the autocorrect plugin needs to have a text field, and we can use database triggers to keep that field in sync with our option sets. So at the moment, I've just got these set to Auto bind, and I'll demonstrate that. So let's correct this one. The Land Rover Discovery. It's auto binding, and so if I refresh the page, the field is updated.
How to use database triggers
Now, I need to add in a database trigger, and to do that, I go to Back end workflows and custom... Here we go. General rather, New database trigger event. And so I will say update vehicle, make and model field. I want this triggered each time a change occurs to a vehicle.
I'm going to build in as I'm going along, a way of optimizing the workflow here, bearing in mind the cost of workload units. So I'm going to say when vehicle before changes make is not vehicle now, make. Or when vehicle before changes model is not vehicle now's model. So without having this only when statement here, the workflow would run every single time any changes made to any field on vehicle. And I want to limit that down, of course, because I don't want to spend unnecessary workflow units.
So now what I can do is I can say, make changes to a thing, make changes to the vehicle now. So with the more recent, the up to date data, and I can create a new field of type text. And so this will be make and model. And I like to have a bit of a convention of putting in text at the end. It's a prompt to me, it reminded me why I have this additional field when I'm already saving make a model. So I can access make and model in the fuzzing search. So this of type text. And so I can say vehicle nows make display. Just click slightly after, add in a space, vehicle nows model display. And I'm going to add in a label into my into my table so we can see this updating. Car. Vehicle. Tasks. There we go. Let's update that.
Okay, and the label is empty because I've not supplied that data. So let's just make some changes.
Okay, and so now we have that text label updated so that it includes the text values of the display labels for our option set. And then this means that we can tap into it with the fuzzy search. And then if I just update the data source for the table, no, not do a search for that's what it was before. I want this to be my search and autocorrect matches. Okay, so there's no results. But if I start typing in Land Rover... Now, I need to tune. This is something that you need to update on your app. The reason that it's a little bit odd is these values here need to be improved depending on your use case, the threshold, the location, distance, et cetera. But you can at least see that it is showing the number one spot as Land Rover because I've typed in Land Rover.
Just a quick thing that I would do to improve this would be to say, you do make this do a search for vehicles, showing all vehicles. In fact, we could then alphabetically sort them by a new field. And then I'd say a conditional value is when the search matches first item is not empty. That's when I change the data source to the plug-in's results. So that should allow me to have all of my results appearing. But then when I start to search for Land Rover, I'm getting the search result.
So there you have it. That is another demonstration of how data based triggers can take a burden off you of a field that you need that you need to ensure is always in sync with two or more other fields. If you've got any questions about data based triggers, leave a comment below. If you need help, you can Bubble out. Head to our website, we do one to one tuition called Bubble coaching.