Cancel insert if item is repeated in the database

I built a form with database dataset, but before insert the item, i want to know how can I cancel de insert the item in database if one of data are repeared with another old data.

Hey,
You can create an onClick event to the submit button instead of connecting it to the dataset,
in the event you can check whether the item exists in the database using query and then use insert to insert the data in the database.
Check out the following:

Good luck :slight_smile:

Thanks… Finally… I needed to try and try a lot of times with queries and diferents parameters. but it is working… thanks…
The code are in spanish but if someone need it… :slight_smile:

export function input3_change(event) {
wixData.query(“Clientes”)
.eq(“noDocumento”, $w(“#input3”).value)
.find()
.then((num) => {
let numberOfItems = num.totalCount;
if (numberOfItems === 0) {
$w(“#text19”).text = “Este numero es Nuevo”;
$w(“#button1”).show(“FadeIn”);
} else {
$w(“#text19”).text = “Este numero ya está registrado:”;
$w(“#button1”).hide(“FadeOut”);
}
})
.catch((error) => {
let errorMsg = error.message;
let code = error.code;
$w(“#text19”).text = "Error: " + errorMsg;
});
}

///////////////////////
if you know another structure, thanks.

regards.

Hi, for more advanced cases, you can also use data hooks, which run in the backend (whenever an item is about to get inserted for example). That way you can always be sure that however an item is being inserted, your safeguard runs.
You can read more about it here: Velo: Using Data Hooks | Help Center | Wix.com

Just in case someone else stumbles upon this post looking for the hook approach.
Check out this other post: