Ensuring KB Articles Always Point to the Latest Version with a Fix Script
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-09-2025 11:51 PM - edited ‎04-10-2025 03:23 AM
Hello!
I had a problem in ServiceNow based on the custom table 'x_vgll_decom_decommission_task'. There is a field called link_to_knowledge of type HTML which contains a link to a KB article. I needed to change it so that it always links to the latest version.
I got help to create a script for the links in a flow editor action activity in ServiceNow. This is how the working script looks:
var knowledgeArticlesStr = fd_data.action_inputs.decommission_template.knowledge_article;
if(knowledgeArticlesStr){
var knowledgeArticlesArr = knowledgeArticlesStr.split(',');
var instance = gs.getProperty('glide.servlet.uri');
var html = "";
knowledgeArticlesArr.forEach(function(kbId){
var kbGr = new GlideRecord('kb_knowledge');
kbGr.get(kbId)
if(kbGr.isValidRecord()){
html += '' + kbGr.getValue('short_description') + '<br>';
}
});
return html;
}
/*
**Access Flow/Action data using the fd_data object. Script must return a value.
**Order number is offset by +1 in Error Handling Section.
**Available options display upon pressing "." after fd_data
**example: var shortDesc = fd_data.trigger.current.short_description;
**return shortDesc;
*/
Here is an example of how a working link (which always shows the latest version) should look: /kb_view.do?sysparm_article=KB0032662
The flow script however, does not update the links for already created records. HTML links for already created activities (x_vgll_decom_decommission_task) hold the old link ending.
So field 'link_to_knowledge' in already created activities needs to be updated so it works like the flow script I showed above, so that they always point to the latest version of KB articles (links that also follow the article itself and not the article's version sys ID). This is how the html link-ending of old recods look:
/kb_view.do?sys_kb_id=3ff586583db41e94f90a3403050af66f&preview_article=true
I tried this fix script to update all records, but it is not working:
(function() {
var taskGR = new GlideRecord('x_vgll_decom_decommission_task');
taskGR.addNotNullQuery('link_to_knowledge');
taskGR.query();
var updatedCount = 0;
while (taskGR.next()) {
var link = taskGR.getValue('link_to_knowledge');
// Försök hitta sys_kb_id i länken
var match = link.match(/sys_kb_id=([a-f0-9]{32})/);
if (match && match[1]) {
var oldSysId = match[1];
// Hämta kb_knowledge-posten baserat på det sys_id
var kbGR = new GlideRecord('kb_knowledge');
if (kbGR.get(oldSysId)) {
var articleNumber = kbGR.getValue('number');
if (articleNumber) {
// Bygg ny länk med sysparm_article istället för sys_kb_id
var newLink = 'https://example-prod.example.se/kb_view.do?sysparm_article=' + articleNumber;
// Uppdatera fältet
taskGR.setValue('link_to_knowledge', newLink);
taskGR.update();
updatedCount++;
}
}
}
}
gs.print('Fix script done. Updated records: ' + updatedCount);
})();
Keep in mind the fix script is in the Global scope, while the table and flow is in a custom application scope. And that the fix script should 'link_to_knowledge' where links exists and not where they are empty.
Thanks in advance!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-11-2025 01:27 AM
@J Siva Now we're talking! It did work now.
However, the issue is now that your script always does not link me to a working KB article.
So this is how a working link should look like to bring me to the latest version always:
But your fix script does a link like this:
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-11-2025 02:06 AM - edited ‎04-11-2025 02:09 AM
@ronro2
Try below script. This will work for sure.
I've modified the second last line.
var updatedCount = 0;
var taskGR = new GlideRecord('x_vgll_decom_decommission_task');
taskGR.addEncodedQuery('link_to_knowledgeISNOTEMPTY');
taskGR.query();
while (taskGR.next()) {
var link = taskGR.getValue('link_to_knowledge');
// Försök hitta sys_kb_id i länken
var match = link.match(/sys_kb_id=([a-f0-9]{32})/);
if (match && match[1]) {
var oldSysId = match[1];
var newLink = this.getKBNumber(oldSysId);
// Uppdatera fältet
taskGR.setValue('link_to_knowledge', newLink);
taskGR.update();
updatedCount++;
}
}
gs.print('Fix script done. Updated records: ' + updatedCount);
function getKBNumber(id) {
gs.info("Inside function: " + id);
// Hämta kb_knowledge-posten baserat på det sys_id
var kbGR = new GlideRecord('kb_knowledge');
if (kbGR.get(id)) {
var articleNumber = kbGR.getValue('number');
if (articleNumber) {
// Bygg ny länk med sysparm_article istället för sys_kb_id
var finalLink = 'https://plexus-prod.vgregion.se/kb_view.do?sysparm_article=;' + articleNumber;
return '<p><a href="' + finalLink + '" rel="nofollow">' + finalLink + '</a></p>';
}
}
}
There's some type in my previous script. As a result multiple "==" were added.
Now it's fixed.