How to determine if you are in Workspace in a Client Script

Community Alums
Not applicable

I'm currently in the process of setting up Agent Workspace to allow users to work on Incidents. We have a number of custom client scripts that run on Incident that i need to configure to work correctly in workspace. Is there a way to determine which UI you are in? I know there is the GlideURI server-side api which i could use to pull apart the uri to determine i'm in workspace but was wondering if there is anything on the client-side i could use? window.location.href doesn't work in workspace either...

 

 

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

you can use the view name i.e. workspace to make client scripts run on workspace

OR

you can check URL

if it contains 'now/agent/workspace' then it's workspace and not native UI

var url = top.location.href;

if(url.indexOf('now/agent/workspace')){

// workspace

}

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

9 REPLIES 9

you can use the workaround I provided.

If my response helped you please mark it correct to close the question so that it benefits future readers as well.

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Recently have been working on this in regards to the SOW so figured it'd be worth updating on this in case people come across it.

You can get the view in a client sided script by getting g_form.getViewName();

Here's an example:

 

var view = g_form.getViewName();
if (view == "sow" || view = "sow_new_record") {
// Put necessary code here
}

 


However, at least for the SOW this was not reliable as not every table has a SOW specific view.
Therefor using top.location.href is more reliable at detecting the SOW or other modern workspaces.

Example:

 

var inSOW = false;

//check url for SOW
var url = top.location.href;
//indexOf returns a numeric value, -1 is not found
if (url.indexOf('/now/sow/') != -1) {
    inSOW = true;
} 

if (inSOW) {
//Put necessary code here
}

 

 

I hope this additional information is helpful to anyone!

If this comment helped you in any way, I would appreciate it if you select it as the correct answer or mark it as helpful.
Thanks!

@smeadows 

Thank you for marking my response as helpful.

If my response helped you please mark it correct to close the question so that it benefits future readers as well.

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Abhishek Bera
ServiceNow Employee
ServiceNow Employee

There is a variable "g_aw" which is only present in the workspace env.

So 

typeof g_aw !== "undefined"  -> true in workspace

tyepof g_aw === "undefined" -> for platform/ native UI

 

 

Hope this helps! 

Fabian10
Tera Guru
I think there's a better way to check if you're in a workspace.
The g_aw API is only availabe in Worspaces, so you can check for that.
 

 

 

 

 

if (typeof g_aw != 'undefined') {
console.log('workspace');
} else {
console.log('not in a workspace');
}