Error Reference

Cannot insert: duplicate value violates unique constraint

Cannot insert: duplicate value violates unique constraint

Where this appears

This error appears in ServiceNow system logs and application logs when executing GlideRecord insert() operations. The error occurs during script execution in Business Rules, Script Includes, or other server-side scripting contexts when attempting to create records that violate unique field constraints.

Sourdough
Chrome Extension

Debugging this in a real instance?

Sourdough lets you trace "Cannot insert: duplicate value violates unique constraint" through system logs, script errors, and API health in the ServiceNow tab you are already using.

Add to Chrome

Free to install. Pro $5/month after a 14-day no-card trial.
Pro requires the ServiceNow admin role. Upgrade inside the extension.

Overview
Tasks
CMDB
API
Metrics
Monitor
Internals
Instance:sourdoughdev·Version:Yokohama
Instance StateONLINE
System StatusFully Operational
Session Timeout90 minutes
Logged-In Sessions2 (20 active)
Build Nameyokohama-12-18-2024_p1
IP Address10.159.128.43
Instance HealthHealth Score: 90%
🔥 5dSourdough (Chrome Plugin)Dark Mode

Cause

Attempting to insert a record with a duplicate value in a field marked as unique in the dictionary, such as sys_id, number field, or custom unique fields. (high likelihood)

Data import or migration scripts creating records without checking for existing values in unique constraint fields like external keys or slug fields. (high likelihood)

Concurrent script execution creating multiple records simultaneously with the same unique field values, causing race conditions. (medium likelihood)

Business Rules or workflows automatically generating values for unique fields that conflict with existing records due to insufficient uniqueness logic. (medium likelihood)

Manual record creation through forms or REST API calls that bypass client-side validation for unique field constraints. (low likelihood)

Fix

Step 1: Check the System Log at System Diagnostics > Log > Application Log to identify the exact table and field causing the unique constraint violation.

Step 2: Navigate to System Definition > Dictionary and search for the problematic field to confirm its unique constraint settings and identify the table involved.

Step 3: Query the target table to find existing records with the duplicate value using a GlideRecord query or list view filter to understand the conflict.

Step 4: Modify your script to check for existing records before insertion using gr.get() or addQuery() methods to validate uniqueness before calling insert().

Step 5: Implement proper error handling in your script using try-catch blocks around GlideRecord operations to gracefully handle constraint violations.

Step 6: If the duplicate record should be updated instead of inserted, modify your logic to use insertOrUpdate() method or conditional update operations.

Step 7: For data imports, implement pre-validation scripts that check for existing unique values and either skip duplicates or generate alternative unique values.

Step 8: Test your modified script in a development instance to ensure it handles both new records and potential duplicates correctly without throwing constraint violations.

Test Your Knowledge

Quick 3-question quiz. See how your ServiceNow skills stack up.

Question 1 of 3Performance

A list view on a table with millions of records is slow. Best fix?

Select an answer to continue