- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-13-2024 01:32 AM
Hello Community,
We have a business use case where we wish to create new knowledge bases and categories in our instance and then update the knowledge base and category for our existing articles.
We have found a script here Migrating knowledge articles between Knowledge Bases via a script - Support and Troubleshooting (ser... which works in principle but there is a caveat at the foot of this article which says you need to do it for all versions. This is where we have the block, does anyone know how we can adapt and leverage this script so that it will make the updates to all versions of the article?
All help appreciated as ever.
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-14-2024 05:22 AM
With this script, you can just switch the if to a while, as the KB number across versions remains the same, but new sys_ids are created.
var TARGET_KB_SYS_ID = '988e3392db5410508c46adc3ca961939';
var TARGET_KB_CATEGORY = '0bc35fc91b7d1950540c1138b04bcbbf';
var KB_ARTICLES_TO_MOVE = [
"KB0011889", //sample KB numbers
"KB0011888"
];
for (var i = 0; i < KB_ARTICLES_TO_MOVE.length; i++) {
moveKBArticle(KB_ARTICLES_TO_MOVE[i]);
}
function moveKBArticle(kbNumber) {
var kb = new GlideRecord('kb_knowledge');
kb.autoSysFields(false);
kb.setWorkflow(false);
kb.addQuery('number', kbNumber);
kb.query();
if(!kb.hasNext()){
gs.info('Did not find ' + kbNumber);
return;
}
while (kb.next()) {
kb.kb_knowledge_base = TARGET_KB_SYS_ID;
kb.kb_category = TARGET_KB_CATEGORY;
kb.update();
gs.info('Moved ' + kbNumber + ' to Knowledge Base ' + TARGET_KB_SYS_ID);
}
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-13-2024 03:02 AM
Are you needing to be selective of what articles to migrate, or is it a case of everything in knowledge base A is now going to knowledge base B. When you do the migration, how are you mapping the articles to their new category?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-13-2024 03:59 AM
Hi Kieran,
It will need to be selective and we would likely be doing a script per category / kb rather than a big bang e.g. article 1, article 3, article 5 are all moving to knowledgebase x and knowledgebasex.category y.
The script provided by ServiceNow in that knowledge article works to do that, but if you have versioning switched on it will only update one record.
var TARGET_KB_SYS_ID = '988e3392db5410508c46adc3ca961939';
var TARGET_KB_CATEGORY = '0bc35fc91b7d1950540c1138b04bcbbf';
var KB_ARTICLES_TO_MOVE = [
"KB0011889", //sample KB numbers
"KB0011888"
];
for (var i = 0; i < KB_ARTICLES_TO_MOVE.length; i++) {
moveKBArticle(KB_ARTICLES_TO_MOVE[i]);
}
function moveKBArticle(kbNumber) {
var kb = new GlideRecord('kb_knowledge');
kb.autoSysFields(false);
kb.setWorkflow(false);
kb.addQuery('number', kbNumber);
kb.query();
if (kb.next()) {
kb.kb_knowledge_base = TARGET_KB_SYS_ID;
kb.kb_category = TARGET_KB_CATEGORY;
kb.update();
gs.info('Moved ' + kbNumber + ' to Knowledge Base ' + TARGET_KB_SYS_ID);
} else {
gs.info('Did not find ' + kbNumber);
}
}
As each version has a unique sys_id this is likely where we would look, so the look up would be based on this rather than number. But unsure how to adapt this script to do that.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-14-2024 05:22 AM
With this script, you can just switch the if to a while, as the KB number across versions remains the same, but new sys_ids are created.
var TARGET_KB_SYS_ID = '988e3392db5410508c46adc3ca961939';
var TARGET_KB_CATEGORY = '0bc35fc91b7d1950540c1138b04bcbbf';
var KB_ARTICLES_TO_MOVE = [
"KB0011889", //sample KB numbers
"KB0011888"
];
for (var i = 0; i < KB_ARTICLES_TO_MOVE.length; i++) {
moveKBArticle(KB_ARTICLES_TO_MOVE[i]);
}
function moveKBArticle(kbNumber) {
var kb = new GlideRecord('kb_knowledge');
kb.autoSysFields(false);
kb.setWorkflow(false);
kb.addQuery('number', kbNumber);
kb.query();
if(!kb.hasNext()){
gs.info('Did not find ' + kbNumber);
return;
}
while (kb.next()) {
kb.kb_knowledge_base = TARGET_KB_SYS_ID;
kb.kb_category = TARGET_KB_CATEGORY;
kb.update();
gs.info('Moved ' + kbNumber + ' to Knowledge Base ' + TARGET_KB_SYS_ID);
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-14-2024 05:39 AM
Thanks Kieran,
I just tested it and it is working perfectly and greatly reduces the work.
Fantastic!