make Serial number field unique

ChetanSh
Tera Contributor

Every time a new record is inserted there should be unique serial number for it.

2 ACCEPTED SOLUTIONS

VedS
Mega Guru

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

var serialNumber = current.serial_number;
    if (serialNumber && serialNumber != '') {
        var gr = new GlideRecord('alm_hardware');
        gr.addQuery('serial_number', serialNumber);
        gr.query();
        if (gr.next() && gr.sys_id != current.sys_id) {
            gs.addErrorMessage('This serial number already exists.');
            current.setAbortAction(true); // Abort the save operation
        }

Please mark this as "correct" and "helpful" if you feel this answer helped you in anyway.

 

Thanks and Regards,

Ved

View solution in original post

Aniket Chavan
Tera Sage
Tera Sage

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:

  1. Navigate to System Definition > Tables

  2. Open your target table

  3. Scroll to the Database Indexes related list → Click New

  4. Choose serial_number as the field

  5. Check the Unique Index checkbox

  6. 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

View solution in original post

4 REPLIES 4

VedS
Mega Guru

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

var serialNumber = current.serial_number;
    if (serialNumber && serialNumber != '') {
        var gr = new GlideRecord('alm_hardware');
        gr.addQuery('serial_number', serialNumber);
        gr.query();
        if (gr.next() && gr.sys_id != current.sys_id) {
            gs.addErrorMessage('This serial number already exists.');
            current.setAbortAction(true); // Abort the save operation
        }

Please mark this as "correct" and "helpful" if you feel this answer helped you in anyway.

 

Thanks and Regards,

Ved

@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! */


Aniket Chavan
Tera Sage
Tera Sage

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:

  1. Navigate to System Definition > Tables

  2. Open your target table

  3. Scroll to the Database Indexes related list → Click New

  4. Choose serial_number as the field

  5. Check the Unique Index checkbox

  6. 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

Ankur Bawiskar
Tera Patron
Tera Patron

@ChetanSh 

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.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader