- Post History
- Subscribe to RSS Feed
- Mark as New
- Mark as Read
- Bookmark
- Subscribe
- Printer Friendly Page
- Report Inappropriate Content
on 07-10-2019 10:40 AM
You may or may not be aware that the number field for records across the platform is not unique by default. Usually this presents no issue, however when importing records it can cause duplicate numbers to be used.
If you only have a handful, manually renumbering works fine. But what if you have 1000+?
Unfortunately ServiceNow doesn't have a shareable solution for this and I was directed to the community for help with a script. There are a few posts about renumbering duplicate records, but nothing specific to knowledge and certainly none that account for versioning (multiple versions of an article share the same number). I used a great post from Allen A to get me started and built off of that to account for the versioning. You can find the original post here.
I was able to develop a working solution and thought I'd share the code in case anyone has the same issue in the future! This would be run as a background script.
getDupes();
function getDupes() {
var dupNum = [];
var ga = new GlideAggregate('kb_knowledge');
ga.addAggregate('COUNT', 'number');
ga.addQuery('number', '!=', '');
ga.groupBy('number');
ga.addHaving('COUNT', '>', 1);
ga.addQuery('workflow_state', 'published'); //comment this out if you have duplicates in draft
ga.query();
while (ga.next()) {
dupNum.push(ga.number.toString());
}
gs.print('The KB table has ' + ga.getRowCount() + ' duplicates based on number field...');
//Loop through duplicate array
for (i=0;i<dupNum.length;i++) {
var gr = new GlideRecord("kb_knowledge");
gr.addQuery("number",dupNum[i]);
gr.query();
while(gr.next()) {
////versions share number and article ID. In the event of duplicate numbers, article ID can be used to identify versions that should retain the same number as its predecessors
var versions = new GlideRecord ('kb_knowledge');
versions.addQuery('article_id', gr.article_id);
versions.addEncodedQuery("article_id!=NULL");
versions.query();
var nm = new NumberManager('kb_knowledge');
var newNum = nm.getNextObjNumberPadded();
while(versions.next()){
versions.number = newNum;
versions.update();
}
}
}
}
- 7,300 Views

- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
Hey Bryan,
First of all, this is awesome! I'm actually looking at how to renumber KBs (I have 100s of dups!!). Just a couple of questions:
1. Will this renumber them for me in order? So basically, if all my KBs are KB000001, will it automatically do the next as 02, 03, etc?
2. Where do I run this? I want to be able to run instantly.


- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
Glad my script could help you along...

- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
This script isn't working for me. :C
When I run it, it just renumbers all of my KBs.


- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
Hi,
Unfortunately, I can't attest to bryanshaw's script itself (it appears as if he took my script (from my post in the link above) and did a few adjustments - those adjustments I can't really speak for).
As far as it renumbering your knowledge articles, that's what it's supposed to do.
Were you expecting something different?

- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
It looks like yours was for INC and he commented saying it doesn't work for that (I did just check out your script). Is this not the case?
But yeah, I have a bunch of KBs called KB0000001 or something and I want them to be fixed and adjust the next one as 02, 03, 04, etc. Does that make sense?


- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
My script works for any table....that has records with "numbers" associated to them...bryanshaw said that it doesn't work for Knowledge (it does) because my script doesn't take versioning in to account...so he then altered it (slightly) to supposedly get that to work (to consider versioning in the renumbering process). That's what this thread is about.
So anyways...the core part of the script, that I had a part in, is to look over the table you specify (incident, demand, project, knowledge, etc.) and look for duplicate records that are literally numbered the exact same. Once it finds those duplicates, it will then pad (number) the duplicates to the next available number.
There are settings in servicenow, for example, where becky, can open an incident in the back-end view, not actually submit it, but open it, and then bail and go...nah...nevermind I don't want to submit...however...since becky opened the form...it "used" that number...so it's no longer available.
So when this padding (numbering) takes place, it's basically using the next number after the last number that was "used"...so it may not take KB0000002...but instead go to KB0000029, then 30, then 31, etc.

- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
Ahh, got it. Where would I run this? As a background script?


- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
Yes, background script. Unfortunately, he accidentally forgot to mention that part if it wasn't a given.
Anyways, if any of my replies have been Helpful, please mark the various ones as Helpful along the way.
Thanks!

- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
Thank you! This was helpful! Going to comment on the OG post too!
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
Thanks guys. Glad you found this helpful. I've updated my post to mention running as a background script and to call out Allen's post specifically as the source of the original code I used to get started.


- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
Thanks
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
Hey Bryan,
This script is not working. If I am running this piece of code in my background script, it is converting all the KB articles to one number i.e. assigning one number to all the KB articles. Whereas, Allen's script is working fine if I use it for kb_knowledge table.
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
Hi Monika,
Are you sure you used the script exactly as posted? I just tried it again in my instance and it's working fine. Are you using article versioning?
Thanks, Bryan
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
Hi Bryan,
I have few questions. My instance has few duplicates. There are few versions as well. To test, I exported few articles in my developer instance to test this background script. It changed numbering for all the KB articles. It kept the same numbers for same KB articles with different versions which is expected. Although I have few concerns:
1. It is changing numbering for all KB articles. My expectation was that it will change numbering only for duplicate articles.
2. We have different instances. The script after running will give different results in different instances.
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
Hi Monika,
I'm not sure about #1, for me it only grabs the duplicates. For #2, you are exactly right. I would recommend running it in your PDI and lower environments until you are satisfied it works to your expectations, then run it in Production and clone down soon there after.
Thanks, Bryan
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
Bryan !! I was testing your code for Enterprise enterprise_strategy table . It fetched the Duplicated Numbers and then it renumbered the affected Enterprise Number !! Thanks
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
Christine Greetings !! I am not sure whether you still require require this piece of update . But you can have check later . I was running the above said Script in my Enterprise table .It worked fine
Before Renumbering Enterprise Number
After Renumbering Enterprise Number
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
Hi Bryan,
I tried same script it updated all articles with same number, please help if I'm doing anything wrong here.
Thank you.
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
Ash,
I'm working through the same problem right now and I think the issue is that the query that Bryan is using to get the duplicate kb numbers is unintentionally including all articles that have had any versions created. So if an article has a version 1.0 and a version 2.0 it will be included in the results because the COUNT of the number will now be greater than 1 even though it's not truly a duplicate as the article sys ids are the same. I think this is the same issue that Monika was experiencing above.
I found by adding the following code to the initial query it looks at just the latest version of each number and then only returns those that have the same number but different sys_ids
ga.addQuery('latest', 'true');
You can run the following under "Scripts - Background" to get a list of duplicate KB numbers and a total count to test this out. Just change the value to 'false' or comment it out to see the different numbers and spot check the articles. It does list each KB Number that is returned in the query so be careful if you have many thousands of duplicates.
var ga = new GlideAggregate('kb_knowledge');
ga.addAggregate('COUNT', 'number');
ga.addQuery('number', '!=', '');
ga.groupBy('number');
ga.addHaving('COUNT', '>', 1);
ga.orderBy('number');
ga.addQuery('latest', 'true'); //limit query to only latest version of article
ga.addQuery('workflow_state', 'published'); //comment this out if you have duplicates in draft
ga.query();
gs.print('The KB table has ' + ga.getRowCount() + ' duplicates based on number field...');
while (ga.next()) {
var numberCount = ga.getAggregate('COUNT', 'number');
var kbNumber = ga.getValue('number')
//Comment the following line out if you just want a total number of dup articles
gs.info('KB Article Number: {0} Count {1}', [kbNumber, numberCount]);
}
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
above script returns the count of duplicate incident number if applied on incident table but how to re- number the duplicate incident number found?
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
Hello,
I have executed Bryan's script and it has made all articles with same number. I am not able see Adam's original post. Can someone help me to fix this?