Duplicate knowledge article numbers
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
4 weeks ago
Hi All
Because of import data and incorrect transform map the knowledge article numbers have been duplicated so as example:
I have two totally different articles but they have the same Number.
As a proposed solution I received this one:
https://www.servicenow.com/community/servicenow-ai-platform-articles/re-numbering-duplicate-kb-artic...
But I see some comments that it is not always working as expected, any idea how to change the Number like KB0012345 in knowledge articles (in this ones which have been loaded with incorrect transform map)?
Thanks in advance for help
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
4 weeks ago
Hi there @Kasia5
If you want to renumber them here is the Background script
var dup = new GlideAggregate('kb_knowledge');
dup.addAggregate('COUNT', 'number');
dup.groupBy('number');
dup.addHaving('COUNT', '>', 1);
dup.query();
while (dup.next()) {
var kbNumber = dup.getValue('number');
var count = parseInt(dup.getAggregate('COUNT', 'number'), 10);
gs.info('Fixing duplicate KB number: ' + kbNumber + ' (' + count + ' records)');
var gr = new GlideRecord('kb_knowledge');
gr.addQuery('number', kbNumber);
gr.orderBy('sys_created_on'); // keep oldest as-is
gr.query();
var first = true;
while (gr.next()) {
if (first) {
first = false;
continue;
}
gr.setValue('number', '');
gr.update();
}
}
This is renumber the KB articles. for explame if you have KB12345 twice, it keeps one with same name and changes the other kb article to diff num.
Kind Regards,
Azar
Serivenow Rising Star ⭐
Developer @ KPMG.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
4 weeks ago
Hi @Kasia5 ,
So I think this background script should work. I have made it so that auto-detects duplicates and renumbers the articles. Hope it works for you
var duplicates = {};
var totalProcessed = 0;
// Phase 1: Find ALL duplicates (newest first)
var gr = new GlideRecord('kb_knowledge');
gr.orderByDesc('sys_created_on');
gr.query();
while (gr.next()) {
var num = gr.getValue('number');
if (!duplicates[num]) duplicates[num] = [];
duplicates[num].push(gr.sys_id.toString());
totalProcessed++;
}
var dupeGroups = 0;
for (var num in duplicates) {
if (duplicates[num].length > 1) {
gs.print('DUPLICATE: ' + num + ' (' + duplicates[num].length + ' records)');
dupeGroups++;
}
}
gs.print('Total records scanned: ' + totalProcessed + ' | Duplicate groups: ' + dupeGroups);
// Phase 2: FIX duplicates (keep newest, renumber others)
var fixedCount = 0;
for (var dupNum in duplicates) {
if (duplicates[dupNum].length <= 1) continue;
// Process this duplicate group
var dupGr = new GlideRecord('kb_knowledge');
dupGr.addQuery('sys_id', 'IN', duplicates[dupNum].join(','));
dupGr.orderByDesc('sys_created_on'); // Newest first
dupGr.query();
var keepThisOne = true;
while (dupGr.next()) {
if (keepThisOne) {
gs.print('KEEPING newest: ' + dupNum + ' (' + dupGr.sys_id + ')');
keepThisOne = false;
continue;
}
// Renumber duplicate (not newest)
var nm = new NumberManager('kb_knowledge');
var newNumber = nm.getNextObjNumberPadded();
dupGr.number = newNumber;
dupGr.setWorkflow(false); // Bypass business rules
dupGr.update();
gs.print('FIXED: ' + dupNum + ' → ' + newNumber + ' (' + dupGr.sys_id + ')');
fixedCount++;
}
}
gs.print(' SUCCESS: Fixed ' + fixedCount + ' duplicate KB articles!');
If my solutions helped you, please mark it as helpful.
Regards,
Manish Singh
