- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-02-2025 12:27 AM
Every time a new record is inserted there should be unique serial number for it.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-02-2025 12:29 AM - edited 07-02-2025 12:31 AM
Hello @ChetanSh ,
You can achieve this using BR.
I have implemented this on Hardware table if yours is also the same you can follow the below steps
set when to run before -- Insert checked
in advanced write the below script
Please mark this as "correct" and "helpful" if you feel this answer helped you in anyway.
Thanks and Regards,
Ved
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-02-2025 12:44 AM - edited 07-02-2025 12:48 AM
Hello @ChetanSh ,
If you want to make the Serial Number field unique for every new record, there are a couple of ways to approach it depending on whether you want to auto-correct duplicates or strictly block them.
🔹 Option 1: Auto-fix with a Business Rule
You can create a "Before Insert" Business Rule that checks if the serial number already exists. If it does, it replaces it with the next available number (so your insert still succeeds). Here's a sample script you can use:
var curNum = current.serial_number + ''; // Adjust field name if different
if (curNum) {
var now_GR = new GlideRecord(current.getRecordClassName());
now_GR.addQuery('serial_number', curNum);
now_GR.setLimit(1);
now_GR.query();
if (now_GR.getRowCount() > 0) {
var newNum = getNextObjNumberPadded(); // Replace with your own number generator
gs.addInfoMessage("Serial Number " + current.serial_number + " already exists. It has been changed to " + newNum);
current.serial_number = newNum;
}
}
✅ This way, your inserts won’t fail — and duplicates are automatically corrected.
Below is the SNOW doc I refered for the above approach.
🔹 Option 2: Enforce Uniqueness with a Table Index
If you want to strictly block duplicate entries, you can create a unique index on the serial_number
field. This ensures the system will reject any record with a duplicate value.
To do this:
-
Navigate to System Definition > Tables
-
Open your target table
-
Scroll to the Database Indexes related list → Click New
-
Choose serial_number as the field
-
Check the Unique Index checkbox
-
Click Create Index
⚠️ Heads-up:
-
Creating indexes impacts performance, especially on large tables — so it's best to do it during off-peak hours.
🔹 Please mark ✅ Correct if this solves your query, and 👍 Helpful if you found the response valuable.
Best regards,
Aniket Chavan
🏆 ServiceNow MVP 2025 | 🌟 ServiceNow Rising Star 2024
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-02-2025 12:29 AM - edited 07-02-2025 12:31 AM
Hello @ChetanSh ,
You can achieve this using BR.
I have implemented this on Hardware table if yours is also the same you can follow the below steps
set when to run before -- Insert checked
in advanced write the below script
Please mark this as "correct" and "helpful" if you feel this answer helped you in anyway.
Thanks and Regards,
Ved
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-02-2025 12:38 AM
@VedS the variable serialNumber would require also some lower x upper case verification: "SN123" versus "sn123". Or automatically make it always upper case to maintain some unified format and convention.
/* If my response wasn’t a total disaster ↙️ ⭐ drop a Kudos or Accept as Solution ✅ ↘️ Cheers! */
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-02-2025 12:44 AM - edited 07-02-2025 12:48 AM
Hello @ChetanSh ,
If you want to make the Serial Number field unique for every new record, there are a couple of ways to approach it depending on whether you want to auto-correct duplicates or strictly block them.
🔹 Option 1: Auto-fix with a Business Rule
You can create a "Before Insert" Business Rule that checks if the serial number already exists. If it does, it replaces it with the next available number (so your insert still succeeds). Here's a sample script you can use:
var curNum = current.serial_number + ''; // Adjust field name if different
if (curNum) {
var now_GR = new GlideRecord(current.getRecordClassName());
now_GR.addQuery('serial_number', curNum);
now_GR.setLimit(1);
now_GR.query();
if (now_GR.getRowCount() > 0) {
var newNum = getNextObjNumberPadded(); // Replace with your own number generator
gs.addInfoMessage("Serial Number " + current.serial_number + " already exists. It has been changed to " + newNum);
current.serial_number = newNum;
}
}
✅ This way, your inserts won’t fail — and duplicates are automatically corrected.
Below is the SNOW doc I refered for the above approach.
🔹 Option 2: Enforce Uniqueness with a Table Index
If you want to strictly block duplicate entries, you can create a unique index on the serial_number
field. This ensures the system will reject any record with a duplicate value.
To do this:
-
Navigate to System Definition > Tables
-
Open your target table
-
Scroll to the Database Indexes related list → Click New
-
Choose serial_number as the field
-
Check the Unique Index checkbox
-
Click Create Index
⚠️ Heads-up:
-
Creating indexes impacts performance, especially on large tables — so it's best to do it during off-peak hours.
🔹 Please mark ✅ Correct if this solves your query, and 👍 Helpful if you found the response valuable.
Best regards,
Aniket Chavan
🏆 ServiceNow MVP 2025 | 🌟 ServiceNow Rising Star 2024
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-02-2025 12:59 AM
you didn't share which table etc?
what type of uniqueness should be there?
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader