63
Views
16
Comments
Solved
Preventing Duplicate Data in a Column in Entity/DB for Both Create and Edit Forms

I'm working on an OutSystems application where I need to prevent duplicate data from being saved in a specific column (e.g., Keyword) in my database.

For a new form:

  • When the user submits data, I want to check if the value already exists in the database. If it does, show an alert message and prevent saving. If it's unique, the data should be saved.

For an edit form:

  • When editing an existing record, I need to ensure that the Keyword field is unique across all records except the current one. If the new value is already taken by another record, show an alert message. If the value is the same as the current record's keyword, allow the update.

Approach:

  1. Using indexes in the entity to enforce uniqueness, but I’m unsure how to customize the alert message for that.
  2. I’m looking for a way to implement the logic in the Client Action or Server Action to check for duplicates and show the appropriate message.

Can anyone provide guidance on:

  • How to implement this logic for both create and edit forms.
  • Whether it's possible to customize the alert message when using an index.
  • If there are any OML samples demonstrating this behavior.

I’m stuck at this point and would really appreciate any help or suggestions!

2023-11-22 10-51-50
Jozy Sohail
Solution

hi Jeremy,

attaching the OML for your reference,

i have done it for one attribute, you can copy the same for all the attributes you want to prevent duplicates.

Hope it helps.

CheckDuplicate.oml
2020-05-21 04-56-19
Santosh Chanveer
Solution
2021-09-06 15-09-53
Dorine Boudry
 
MVP

Hi @Jeremy Hui ,

adding the index is the good approach to safeguard the data quality, as a last resort, a database error will come saying the insert or update is not possible.  But you can't (or shouldn't want to try) change the message coming with that error.

So the approach is to do a validation before doing the create or the update, and this would typically be done inside the CRUD wrapper.

I would say, in it's most simple form, you have a single validation that works both for create and update, being an aggregate finding a record with a different id but with the same keyword.  If found, raise your own user exception with your custom message.

Dorine

UserImage.jpg
Jeremy Hui

May i Know how can u do that or do u have any sample OML?

2025-01-23 09-22-22
ABHIJITH G
Champion

Hi Jeremy Hui,

Here is a sample to check the duplicate data in the database. Here in the sample, I am checking whether there is a record already existing I am raising an exception InvalidInput, in which we can customize the error message.

Thanks.

UserImage.jpg
Jeremy Hui

Can you share for the OML?

UserImage.jpg
Jeremy Hui

Hi @Rajat Agrawal ,

How about the error message for it?

2021-11-12 04-59-31
Manikandan Sambasivam

Hi,

We can handle this using a Server Action to check for duplicate values.

In the Server Action, validate the Keyword field by passing the new keyword as input. For edit scenarios, include the current record ID. Use an Aggregate with a filter such as Keyword = Input_Keyword AND Id <> Input_CurrentRecordId.

Additionally, in the Entity, add a unique index to the Keyword column to enforce uniqueness at the database level. Handle errors through exception handling.

2020-05-21 04-56-19
Santosh Chanveer

Hi ,

Scenario -1 

Since you are using index so whenever you hit entity with duplicate data you will get Error executing query. 

Assuming you want to capture/ customize  the error thrown by create or update entity  action ,

1. In wrapper action where you are using entity action you can add handler to capture  the DB exception

create structure output to handle error and set the IsError property to  False when db operation is successful and True in error handler .

2. In SaveData client action Based on the error flag returned by wrapper action you can display feedback / error message to user . By following this no default DB errors will be displayed on screen 

Scenario 2-

If you are using aggregate/ data action on screen then you can validate if record is exist with same data by using filters and display necessary feedback message as suggested on solutions above .

Thanks,

Santosh

UserImage.jpg
Jeremy Hui

able to share some sample OML?

2020-05-21 04-56-19
Santosh Chanveer
Solution
DB_Exception.oml
2025-09-25 14-38-22
Lokesh Kumar Yadav

To implement duplicate prevention for the Keyword column in your OutSystems application, you can achieve this in two steps: checking for duplicates in the database and displaying appropriate alerts. Below is a step-by-step guide tailored for both Create and Edit forms.

  • Add a unique index to the Keyword column in your database entity to ensure that duplicates cannot be saved at the database level.
  • Note: If you go this route, handling custom alerts requires additional logic, as the index violation will throw a generic error.
  • Create a Server Action to check if the Keyword value exists in the database.
  • Use an aggregate to filter the Keyword column
  • Call the Server Action to check for duplicates before saving.
  • If IsDuplicate is True, show a Client Action (e.g., alert or feedback message) and stop the flow.
  • If IsDuplicate is False, proceed with saving the record.
UserImage.jpg
Jeremy Hui

any sample OML for reference?

2024-05-14 05-39-17
Jothikarthika - EONE

Hi Jeremy Hui,

I've shared the OML for your requirement. I believe it will be helpful to you. Demolink

Best regards,

Jothikarthika

DuplicatePrevent.oml
2022-12-30 07-28-09
Navneet Garg

you can add unique index to your column and handle it your logical flow.

2023-11-22 10-51-50
Jozy Sohail
Solution

hi Jeremy,

attaching the OML for your reference,

i have done it for one attribute, you can copy the same for all the attributes you want to prevent duplicates.

Hope it helps.

CheckDuplicate.oml
2025-09-25 14-38-22
Lokesh Kumar Yadav

If you found the solution helpful, please mark it as a solution. 

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