Abort duplicate insert

SandeepKSingh
Kilo Sage

In CMDB_CI table I need to avoid duplicate data inserstion.

Can it be achived via Discovery or do we need to write a script??

2 ACCEPTED SOLUTIONS

abirakundu23
Mega Sage

Hi @SandeepKSingh ,

You can restrict duplicate CIs in 'cmdb_ci' table in both ways.

1. Discovery - If you follow discovery approach then update existing CIs instead of creating same new record, so that have to build Identification rule. 
Doc Link : 

Identification rules (servicenow.com)
CMDB Identification and Reconciliation rules | CMDB IRE ServiceNow | Part 1 - YouTube
ServiceNow CMDB Identification Rule Demo | CMDB IRE Live Demo (youtube.com)

 

2. Custom Script - We can create before /insert custom BR on 'cmdb_ci' table & also restrict the duplicate CI's entry in 'cmdb_ci' table.

(function executeRule(current, previous /*null when async*/) {

//insert or update operation check

if (current.operation() == 'insert' || current.operation() == 'update')

{

var ciCheck = new GlideRecord('cmdb_ci');

ciCheck.addQuery('serial_number', current.serial_number);

ciCheck.query();

if (ciCheck.next())

{

gs.addErrorMessage(' CI Record with this serial number already exists in table.');

current.setAbortAction(true); // Abort the insert operation

}

}





Please mark helpful & accept answer if it's really worthy for you.

View solution in original post

ServiceNow CMDB Identification Rule Demo | CMDB IRE Live Demo Follow The Complete CMDB Series :- https://www.youtube.com/playlist?list=PLrmQ9R9YkZmtHdO5rxriLngahJMgCB1mx Get the scripts from below article - https://servicenowspectaculars.com/servicenow-cmdb-identification-rule-demo/ Enjoy the ...

Ravi Gaurav
Giga Sage
Giga Sage

Hi @SandeepKSingh 

 

---- >Avoiding Duplicates via Discovery:

Discovery in ServiceNow is designed to automatically detect and handle duplicates in the CMDB. It uses identifiers and reconciliation rules to prevent duplicate entries.

  • Identifiers and Reconciliation Rules:
    • Identifiers: Discovery uses identifiers to determine if a CI already exists in the CMDB. These identifiers are a set of attributes that uniquely identify a CI (e.g., serial number, hostname).
    • Reconciliation Rules: These rules determine which source (e.g., Discovery, Import Sets) has authority to update specific fields on a CI.

When Discovery runs, it checks the identifier rules for the CI class it’s populating. If it finds a CI that matches the identifiers, it updates that CI rather than creating a new one, thereby avoiding duplicates.

  • Steps to Configure:
    1. Navigate to Configuration > CI Class Manager.
    2. Select the appropriate CI class (e.g., cmdb_ci_computer).
    3. Review and modify the identifiers to ensure they correctly match the unique properties of your CIs.
    4. Set up reconciliation rules if needed, under Configuration > Reconciliation.

Advantages:

  • Automated: Minimal manual intervention.
  • Consistent: Ensures consistency across CIs based on defined rules.

---- >Avoiding Duplicates via Scripting:

In cases where Discovery is not sufficient or you need additional logic, you can use scripting (like a Business Rule) to prevent duplicate entries.

Example Business Rule: You can create a Business Rule on the cmdb_ci table that checks for duplicates before inserting a new CI.

  • Trigger: Before Insert
  • Condition: None (applies to all insert operations)
  • Script:
    (function executeRule(current, previous /*null when async*/) { // Query the cmdb_ci table to check for a duplicate entry var ci = new GlideRecord('cmdb_ci'); ci.addQuery('name', current.name); ci.query(); // If a record with the same name exists, prevent insert if (ci.next()) { gs.addErrorMessage('Duplicate CI detected: A CI with the name "' + current.name + '" already exists.'); current.setAbortAction(true); // Prevent the insert } })(current, previous);
--------------------------------------------------------------------------------------------------------------------------


If you found my response helpful, I would greatly appreciate it if you could mark it as "Accepted Solution" and "Helpful."
Your support not only benefits the community but also encourages me to continue assisting. Thank you so much!

Thanks and Regards
Ravi Gaurav | ServiceNow MVP 2025,2024 | ServiceNow Practice Lead | Solution Architect
CGI
M.Tech in Data Science & AI

 YouTube: https://www.youtube.com/@learnservicenowwithravi
 LinkedIn: https://www.linkedin.com/in/ravi-gaurav-a67542aa/

View solution in original post

3 REPLIES 3

AndersBGS
Tera Patron
Tera Patron

Hi @SandeepKSingh ,

 

have you identified why data is injected multiple times? Is data only inserted from discovery?

 

my answer has helped with your question, please mark my answer as accepted solution and give a thumb up.

 

Best regards

 Anders

If my answer has helped with your question, please mark my answer as the accepted solution and give a thumbs up.

Best regards
Anders

Rising star 2024
MVP 2025
linkedIn: https://www.linkedin.com/in/andersskovbjerg/

abirakundu23
Mega Sage

Hi @SandeepKSingh ,

You can restrict duplicate CIs in 'cmdb_ci' table in both ways.

1. Discovery - If you follow discovery approach then update existing CIs instead of creating same new record, so that have to build Identification rule. 
Doc Link : 

Identification rules (servicenow.com)
CMDB Identification and Reconciliation rules | CMDB IRE ServiceNow | Part 1 - YouTube
ServiceNow CMDB Identification Rule Demo | CMDB IRE Live Demo (youtube.com)

 

2. Custom Script - We can create before /insert custom BR on 'cmdb_ci' table & also restrict the duplicate CI's entry in 'cmdb_ci' table.

(function executeRule(current, previous /*null when async*/) {

//insert or update operation check

if (current.operation() == 'insert' || current.operation() == 'update')

{

var ciCheck = new GlideRecord('cmdb_ci');

ciCheck.addQuery('serial_number', current.serial_number);

ciCheck.query();

if (ciCheck.next())

{

gs.addErrorMessage(' CI Record with this serial number already exists in table.');

current.setAbortAction(true); // Abort the insert operation

}

}





Please mark helpful & accept answer if it's really worthy for you.

ServiceNow CMDB Identification Rule Demo | CMDB IRE Live Demo Follow The Complete CMDB Series :- https://www.youtube.com/playlist?list=PLrmQ9R9YkZmtHdO5rxriLngahJMgCB1mx Get the scripts from below article - https://servicenowspectaculars.com/servicenow-cmdb-identification-rule-demo/ Enjoy the ...

Ravi Gaurav
Giga Sage
Giga Sage

Hi @SandeepKSingh 

 

---- >Avoiding Duplicates via Discovery:

Discovery in ServiceNow is designed to automatically detect and handle duplicates in the CMDB. It uses identifiers and reconciliation rules to prevent duplicate entries.

  • Identifiers and Reconciliation Rules:
    • Identifiers: Discovery uses identifiers to determine if a CI already exists in the CMDB. These identifiers are a set of attributes that uniquely identify a CI (e.g., serial number, hostname).
    • Reconciliation Rules: These rules determine which source (e.g., Discovery, Import Sets) has authority to update specific fields on a CI.

When Discovery runs, it checks the identifier rules for the CI class it’s populating. If it finds a CI that matches the identifiers, it updates that CI rather than creating a new one, thereby avoiding duplicates.

  • Steps to Configure:
    1. Navigate to Configuration > CI Class Manager.
    2. Select the appropriate CI class (e.g., cmdb_ci_computer).
    3. Review and modify the identifiers to ensure they correctly match the unique properties of your CIs.
    4. Set up reconciliation rules if needed, under Configuration > Reconciliation.

Advantages:

  • Automated: Minimal manual intervention.
  • Consistent: Ensures consistency across CIs based on defined rules.

---- >Avoiding Duplicates via Scripting:

In cases where Discovery is not sufficient or you need additional logic, you can use scripting (like a Business Rule) to prevent duplicate entries.

Example Business Rule: You can create a Business Rule on the cmdb_ci table that checks for duplicates before inserting a new CI.

  • Trigger: Before Insert
  • Condition: None (applies to all insert operations)
  • Script:
    (function executeRule(current, previous /*null when async*/) { // Query the cmdb_ci table to check for a duplicate entry var ci = new GlideRecord('cmdb_ci'); ci.addQuery('name', current.name); ci.query(); // If a record with the same name exists, prevent insert if (ci.next()) { gs.addErrorMessage('Duplicate CI detected: A CI with the name "' + current.name + '" already exists.'); current.setAbortAction(true); // Prevent the insert } })(current, previous);
--------------------------------------------------------------------------------------------------------------------------


If you found my response helpful, I would greatly appreciate it if you could mark it as "Accepted Solution" and "Helpful."
Your support not only benefits the community but also encourages me to continue assisting. Thank you so much!

Thanks and Regards
Ravi Gaurav | ServiceNow MVP 2025,2024 | ServiceNow Practice Lead | Solution Architect
CGI
M.Tech in Data Science & AI

 YouTube: https://www.youtube.com/@learnservicenowwithravi
 LinkedIn: https://www.linkedin.com/in/ravi-gaurav-a67542aa/