How to create a UI Action in Workspace that redirects to the kb_view table?

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-16-2023 09:57 PM
I am trying to create a UI Action for Workspace that will allow me to run a server script and also redirect to the "kb_view" table. When looking at the URL below it seems that it is on the kb_view table and you need to pass the parameters of "sys_kb_id" and "preview_article.
Example URL view knowledge article on kb_view:
instance.service-now.com/now/nav/ui/classic/params/target/kb_view.do%3Fsys_kb_id%3D1d58dd111b2dad5008d497d8b04bcbdf%26preview_article%3Dtrue
I tried to make the UI Action run on the client side and enabled Workspace Form Button and Format for Configurable Workspace. I found that there is a g_aw.openRecord call, but I could not get it to work. I tried several iterations of passing the sys_id in the second parameter, to passing it empty below, and passing extra parameters, but I could not get it to redirect correctly. I would appreciate any ideas on how I could get a UI Action to run a script and redirect to the kb_view table in Workspace.
g_aw developer docs:
https://developer.servicenow.com/dev.do#!/reference/api/tokyo/client/GlideAgentWorkspaceAPI
Thank you!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-16-2023 10:04 PM
Hi @Forrest Falk ,
Here's an example UI Action script that runs a server-side script and redirects to the "kb_view" table in Workspace:
function redirectToKbView() {
var kbId = g_form.getValue('knowledge_base'); // Replace with your own field name that holds the knowledge base ID
// Run server-side script
var ga = new GlideAjax('MyServerScript'); // Replace with your server script name
ga.addParam('sysparm_name', 'myFunction');
ga.addParam('sysparm_kb_id', kbId);
ga.getXMLWait();
// Redirect to kb_view
var kbViewUrl = 'kb_view.do?sys_kb_id=' + kbId + '&preview_article=true';
g_aw.openRecord('', 'kb_view', kbId, kbViewUrl);
}
redirectToKbView();
In this example, replace "MyServerScript" with the name of your server script, and "knowledge_base" with the name of the field on the form that holds the knowledge base ID.
This script first retrieves the knowledge base ID from the form, then runs a server-side script using GlideAjax. After the server-side script completes, it constructs the URL for the "kb_view" table and redirects to it using the g_aw.openRecord() function.
If my response helps you to resolve the issue close the question by ✅Accepting solution and hit 👍thumb icon. From Correct answers others will get benefited in future.
Thanks,
Ratnakar

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-20-2023 02:33 PM
Hi Ratnakar,
Thank you so much for your help! I was able to get the code working on my instance. The only thing I am trying to figure out now is if I can have it refresh the tab it opens. It will open the kb_view tab on the workspace. However, if a user updates the article, leaves the tab open, and clicks the UI Action again, it will redirect to the tab, but will not refresh it. Do you know if there is a way to make it refresh the tab?
Code I ended up using in my UI Action script:
//Get the current records knowledge base ID from the knowledge_base fields.
var kbId = g_form.getValue('knowledge_base');
//Build URL for kb_view
var kbViewUrl = 'kb_view.do?sys_kb_id=' + kbId + '&preview_article=true';
//Open new tab on workspace
g_aw.openRecord('', 'kb_view', kbId, kbViewUrl);
Thank you,
Forrest Falk
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-20-2023 11:10 PM
Hi @Forrest Falk ,
To refresh the tab that opens the kb_view table in ServiceNow Workspace, you can use the following code after opening the record:
var iframe = document.getElementById("tabContentFrame0"); // Replace the frame number with the correct one for your instance
if (iframe) {
iframe.contentWindow.location.reload(true);
}
So your final code for the UI Action script would be:
// Get the current record's knowledge base ID from the knowledge_base field.
var kbId = g_form.getValue('knowledge_base');
// Build URL for kb_view.
var kbViewUrl = 'kb_view.do?sys_kb_id=' + kbId + '&preview_article=true';
// Open new tab on workspace.
g_aw.openRecord('', 'kb_view', kbId, kbViewUrl);
// Refresh tab content.
var iframe = document.getElementById("tabContentFrame0"); // Replace the frame number with the correct one for your instance
if (iframe) {
iframe.contentWindow.location.reload(true);
}
This code should refresh the tab content every time the UI Action is clicked, even if the tab was already open.
Thanks,
Ratnakar

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-22-2023 08:12 AM - edited 03-22-2023 01:53 PM
Hi Ratnakar,
I just realized that my UI Action is not working. I mixed it up with a declarative action that had the same name on my instance. I changed the name of my UI Action to "View Article WP" for testing purposes. After testing the code I noticed that the g_aw.openRecord() function is not working. When I checked the console logs, it says that it requires a table and sys_id. I believe it is throwing this since the first parameter of the g_aw.openRecord() is expecting a table name. I tried to change it to g_aw.openRecord('kb_view', kbId,''), and it now popups up a tab, but says no record found.
Do you know if there is a way to format the UI Action to open a tab for kb_view?
Workspace Client Script Running:
function onClick(g_form) {
//Get the current records knowledge base ID from the knowledge_base fields.
var kbId = g_form.getValue('kb_knowledge_base');
g_form.addInfoMessage('kbId: ' + kbId);
//Build URL for kb_view
var kbViewUrl = 'kb_view.do?sys_kb_id=' + 'kbId' + '&preview_article=true';
g_form.addInfoMessage('kbViewUrl: ' + kbViewUrl);
//Open new tab on workspace
g_aw.openRecord('', 'kb_view', kbId, kbViewUrl);
}
Error thrown in console:
New tab opened, but record not found when trying g_aw.openRecord('kb_view', kbId,'').
Debug messages working in script.
Thank you for all your help!