You can do this one of two ways:
Option 1: Load the full table
If your app is already using a GET query that gets your entire leads table you could set a JS Query to be the form submit handler that checks if the lead already exists.
I'd recommend using _.find()
which means your code would end up looking something like this:
// Import Leads Table
let leads = getLeads.data;
// Import Form value (it will get wiped when you submit and you need it for next steps)
let form = newLeadForm.data;
let newVAT = form.vat_id;
// Check if lead is a duplicate
let findLead = _.find(leads, {vat_id: newVAT);
// If findLead isn't undefined, that means it's already in your database
if(typeof findLead != 'undefined') {
// TODO: OPEN MODAL OR SHOW NOTIFICATION
} else {
// TODO: NEXT STEPS TO SAVE LEAD TO DB
}
The nice thing about doing it this way is that you load the leads database once and then you can add as many leads as you'd like without having to query the leads table again. The main disadvantage though is that it doesn't scale super well. If your leads table ends up with 10k records, you need to lead THE ENTIRE table in your client which can slow it down.
Option 2: Query the individual row
This option is also pretty straightforward but in practice a bit more difficult to implement. You need to do the following things:
- On submit, query your tables database to see if there's any records with the VAT number entered by the user.
- Check if the query returns anything
- If so, show the duplicate modal otherwise save the lead to the db.
You could chain those using success handlers but it gets messy quickly and gets pretty difficult to troubleshoot quickly so I would recommend wrapping everything in a JS Query, meaning you'll need to use async/await. Your JS Query would end up looking something like this:
// Import Form value (it will get wiped when you submit and you need it for next steps)
let form = newLeadForm.data;
let newVAT = form.vat_id;
// Use this syntax to be able to reference the results of an async/await query
return (async () => {
// Query leads table (await means the rest of your code won't execute until this query is resolved)
let findLead = await findLeadQuery.trigger({
additionalScope: {
vat_id: newVAT
}
})
// Check result (find what a negative looks like: null, undefined, [], "", etc. I'll assume it's null)
if(findLead == null) {
// TODO: NEXT STEPS TO SAVE LEAD TO DB
} else {
// TODO: SHOW MODAL OR NOTIFICATION FOR DUPLICATE
}
})();
By default, your JS Query will timeout after 10s, if you know your total queries will exceed that time, make sure to increase the timeout limit in the advanced settings for your query.
Although this approach is a bit more complicated, the nice thing is that the performance will remain the same regardless of the size of your leads database.
Hope this helps!