Find your people. Pick a challenge. Ship something real. The CreatorCon Hackathon is coming to the Community Pavilion for one epic night. Every skill level, every role welcome. Join us on May 5th and learn more here.

message shouldnt come in cab workbench

ServiceNow10sun
Giga Guru

I wan to restrict message in cab workbench page , however i am not getting  message anywhere not in classic UI not in SOW  not in cab workbench , however i need message in classic UI and in SOW .

 

function onLoad() {
    var state = g_form.getValue("state");

    var url = window.location.href;
    var path = window.location.pathname;

    var isCABWorkbench = (url.indexOf("cab_workbench") !== -1) || (path === "/cab");

    // Stop only for CAB Workbench
    if (isCABWorkbench) {
        return;
    }

    if (state === "-5" || g_form.isNewRecord()){
    g_form.addinfomessage("Hi");
    }
}
6 REPLIES 6

kateparker7
Giga Contributor

The reason your script isn't working is that window.location is generally blocked or unreliable in modern Workspaces and the CAB Workbench due to security "sandboxing."

To achieve this, you should use the g_form.getControl() or the glide.ui.can_debug_ajax approach, but the most reliable way in ServiceNow is to check the User Experience context.

Try replacing your URL logic with this:

JavaScript
function onLoad() {
// Check if we are in the CAB Workbench by checking the URL or a specific element
var isCAB = window.top.location.href.includes('cab_workbench');

if (isCAB) {
return;
}

var state = g_form.getValue("state");
if (state === "-5" || g_form.isNewRecord()) {
g_form.addInfoMessage("Hi");
}
}
A few tips to ensure it works across all UIs:

UI Type: Make sure the Client Script "UI Type" is set to All. If it's set to "Desktop," it won't run in SOW or the Workbench.

Isolate Script: Ensure the "Isolate script" checkbox on the Client Script record is unchecked. This allows the script to access the window object, though keep in mind this is not best practice for long-term upgrades.

Alternative: If you want a "cleaner" way without using window, check for a specific field or element that only exists in the Workbench view, or use a View-specific Client Script if you have a dedicated CAB view.

The message in SOW is appearing but its going of in 4 seconds is there any other way that we can keep that message for some time until the user click on cross , like how addinfo message in native UI appears.