How to check if I already have one element on data base?
Application Type
Traditional Web

Hey! So, I am building an app to register contacts and I don´t want to have the same number twice or more times. How do I check If the number already exists? I have an EditRecord to create/Update new numbers

Solution

Hi,

First, if you want that value to be unique, create an Index in your database for that attribute set as Unique:


After that, if you want to manage that use case in your logic to be more "user friendly", in your action that saves the record, first, get a record with the same number. If you get an empty aggregate, it's ok to save. If it's not empty, trigger a message and don't save. You can log that info too if you want.



Hope this can help.


Best regards,

Ricardo M Pereira

Ricardo Pereira was perfect! that's the solution!

Just to complement, in the second part of Ricardo's solution, you can do what he advise or just treat databaseError. 'Cause you had already create an index in the table, so if you try register some contact with the same number you will get a databaseError. Treat it and display a message to the user.

Community GuidelinesBe kind and respectful, give credit to the original source of content, and search for duplicates before posting.