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:
For an edit form:
Approach:
Can anyone provide guidance on:
I’m stuck at this point and would really appreciate any help or suggestions!
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.
Please check oml.
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
May i Know how can u do that or do u have any sample OML?
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.
Can you share for the OML?
Hi @Jeremy Hui
Refer below link for indexing.
https://success.outsystems.com/documentation/11/building_apps/data_management/data_modeling/create_an_entity_index/
Regards ,
Rajat
Hi @Rajat Agrawal ,
How about the error message for it?
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.
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
able to share some sample OML?
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.
any sample OML for reference?
I've shared the OML for your requirement. I believe it will be helpful to you. Demolink
Best regards,
Jothikarthika
you can add unique index to your column and handle it your logical flow.
If you found the solution helpful, please mark it as a solution.