Avoid duplicate insert on custom table
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-13-2023 11:53 AM
Hi ,
i have created one custom table and i am inserting records through api.
here my issue is while inserting records the duplicate records also inserting, so i want to restrict duplicate records for the table
i have return one before insert br for this in search of community, but is not working fine.
below is the code which i have used:
your help will appreciated
(function executeRule(current, previous /*null when async*/ ) {
var gr = new GlideRecord('u_school');
gr.addQuery('u_section', current.u_section);
gr.query();
if (gr.next()) {
current.setAbortAction(true);
gs.addErrorMessage(' A duplicate record was found with the section . Please validate and try again.');
}
})(current, previous);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-13-2023 01:33 PM
Seems the BR is defined on the 'u_school' table, and the logic prevents a new record with the same value for 'u_section'. Logic looks correct if that is your intent. You can debug the logic using the Script Debugger, or add 'gs.info()' lines to see such as
at the top in the function:
gs.info("u_schoolDuplicates: Checking for existing record with u_section: " + current.u_section);
inside the "if":
gs.info("u_schoolDuplicates: Found existing record.");
and then you can use the Script Log Statements to check the logic. Search 'message', 'starts with', 'u_schoolDuplicates'

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-13-2023 02:12 PM
If you do not mind getting an ugly error message you can just create a unique index on the column or columns and the database will bark if there is a duplicate value. The message will just not be very user friendly.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-13-2023 04:29 PM
Perhaps implement both the BR and the Unique index.
The latter has the advantage that - unlike the former - it cannot be circumvented.
BRs can be deactivated in code - than no more checks.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-13-2023 02:55 PM
I agree, creating a unique index for that field would work, but is ugly as stated. Seems easy to test the BR by trying to create a new record from the UI. And use and existing value for 'u_section'. What is present can be seen from a list view of u_school records and then group by the u_section field. Maybe there are other field values for the "duplicates" and needs to be considered. If the BR does work, then it seems the "inserting records through api" needs to be looked at.