Preventing Duplicate Entries

Prev Next

Introduction

When entering data, it sometimes happens that a user is unaware of an existing record with the same name in the database. This can result in the creation of duplicate entries, which complicates data management and reduces data reliability. A simple and effective solution can be implemented to address this issue: a warning dialogue that only appears when a record with the same name is detected.

The ultimate goal is to create a system that displays a warning message when a potential duplicate is detected while allowing the user to decide whether the record should be blocked or accepted. This approach ensures better data control and helps prevent unnecessary errors during the creation of new records.

 

Step-by-step instructions

1. Add a dialogue to the creation command

2. Configure the error message to display

A field must be created within the dialogue and named "messageErreur." This field should be positioned directly after the name field. The order is important, as it ensures the message will appear directly below the "name" field (see the Final Result section).

In the ComponentId property of the new field, add the following value:
@esm/components/web/MessageBar.
This will create a styled box for displaying the message.

Next, in the Props property, add the following JavaScript configuration:

javascriptCopyEditreturn {description: "An account with the same name already exists", type: 'warning'}

The text under "description" is the content of the message, while the "type" property defines the style of the box (e.g., a warning style).

At this stage, the error message will always be displayed.

3. Make the message visible only when a duplicate is detected

To ensure the message appears only when a duplicate is detected, modify the visibility property of the "messageErreur" field using the following JavaScript expression:

db.Account.search({name: {insensitiveEqual:  values.name}}).length > 0 

With this configuration, the dialogue will only appear when a record with the same name already exists.

(Optional) Blocking the creation of duplicate records

In certain cases, it may be necessary to prevent the saving of a record when a duplicate is detected. However, this depends on the context. For example, in the case of client names, there could be two different individuals with the same name. The user will need to exercise their judgment.

To block the creation of duplicates, modify the dialogue options to include the following condition in the Can Submit event:

return () => {
  return !db.Account.search({name: {insensitiveEqual: values.name}}).length > 0;
  }

Conclusion

Implementing a warning dialogue to alert users of potential duplicates improves data quality and helps prevent errors. Depending on specific needs, the system can either block or simply flag the creation of duplicate entries, leaving the final decision to the user.