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-10-2025 02:29 AM
It's working fine for me.
Have you rolled back your previous script execution?
If not, then please do that. And try again.
1. Search "Script execution history" from filter navigator.
2. Open the last executed script and rollback
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-10-2025 03:54 AM
@J Siva I can see other programmers in "Script execution history", but I'm not present there, nor is the script. So it does not exist in the list, therefore I cannot rollback.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-10-2025 04:02 AM - edited ‎04-10-2025 04:02 AM
If you have checked the "Record for rollback" checkbox in your fix script, then you will be able to roll back.
Are you doing this directly in PROD?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-10-2025 04:25 AM
Yes it is checked for rollback.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-10-2025 04:34 AM
Hmm ok. Could you please check that in the "Rollback context"?
Apologies for the confusion.