The CreatorCon Call for Content is officially open! Get started here.

Service Portal VS Platform UI - window === null NOT WORKING

G24
Kilo Sage

Smart folks,

It's embarrassing that I have to ask this, because I thought I knew the answer, and I though the answer worked, but...

 

I have multiple sources telling me the same thing, that I can use the following code to determine, in script, at runtime, if the user is in the Service Portal or in the Platform UI:

if (window === null)

But that is NOT working for me.  What am I doing wrong???

 

Catalog Client Script:

001.png

 

Portal Behavior (as expected):

0002.png

 

Requested Item Behavior (NOT what I was expecting):

0004.png

 

Catalog Task Behavior (also NOT what I was expecting):

0004.png

 

Sources:

https://support.servicenow.com/kb?id=kb_article_view&sysparm_article=KB0744257 

https://www.servicenow.com/community/now-platform-forum/in-client-scripts-how-to-check-where-the-for... 

https://docs.servicenow.com/bundle/rome-servicenow-platform/page/build/service-portal/concept/unsupp... 

https://www.servicenow.com/community/developer-forum/how-to-script-if-serviceportal-client-script/m-... 

 

1 ACCEPTED SOLUTION

Alex Tod1
Kilo Sage

Hello @G24,

 I leave you here an interesting article that have to clarifies the global variable thing: https://www.servicenow.com/community/developer-articles/top-window-document-objects-in-native-ui-and...

 You forgot to check that the isolate script flag is set to false.

View solution in original post

9 REPLIES 9

Hi, @-O- What sort of client scripts are we talking about?  OnLoad()?

 

I can create one OnLoad() Catalog Client Script, and set it to "Desktop", and then I can create another OnLoad() Catalog Client Script and set it to "Mobile / Service Portal".  But then what?

 

The goal is for all of my OTHER Catalog Client Scripts to be able to EASILY determine if they are running in the context of the Portal (Catalog Item View) or the context of the Platform UI (Requested Item or Catalog Task).  I'm not clear on how all of those other scripts would utilize the 2 scripts that you suggests creating.  Will those 2 scripts set some kind of a Global variable or something?  That will then be accessible from the other scripts?

 

Sorry to be dense, but I'm just really not getting it.  Thank you.

-O-
Kilo Patron

Well, you should be able to set a flag on g_scratchpad in the 2 onLoad (Catalog) Client Scripts, but I'd argue, if ALL your scripts need to know whether they are running in Portal or Core UI, you might as well create two versions of ALL those scripts.

I mean you say you want to stay "clean".
Well, separating (Catalog) Client Scripts into Portal and Core UI specific ones using field UI type is the de-facto clean way to do it.

On the other hand I wonder why do you need ALL scripts to know the environment?

That should only be necessary if you are using not-clean techniques down the road.

Just out of curiosity, why would you need to know in ALL scripts whether it is running on Portal or in Core UI?

 

Later edit: also how come you say you have your solution, but you keep asking questions?

Hello, @-O- I posted another article here which might provide more context about why I have these questions.

 

The use of g_scratchpad sounds interesting.  Is there a way I can set that flag and make sure that the setting of the flag runs before all other scripts?  If I set the script order to 0 or 1, will that be the case?  Or will I have to make sure that every other script also has an order?  Because right now most of our scripts don't have an order value set.

 

In your opinion should there NOT BE a UI Type of "All"?  Is that leading developers down the wrong path, because what they should be doing is having 2 separate versions of each script, one that needs to run on the Platform VS and one that runs on the Portal???

 

(And I'm asking follow-up questions so that I can have a better understanding.  I only have the solution for the very specific question that I asked, but I still don't have a good understanding of how ServiceNow is expecting people to do things.  It seems like they don't guide people in the right direction most of the time.  But maybe it's me.)

-O-
Kilo Patron
The use of g_scratchpad sounds interesting. Is there a way I can set that flag and make sure that the setting of the flag runs before all other scripts? If I set the script order to 0 or 1, will that be the case? Or will I have to make sure that every other script also has an order? Because right now most of our scripts don't have an order value set.

You will have to set the order on all scripts that need to run in a given order

In your opinion should there NOT BE a UI Type of "All"? Is that leading developers down the wrong path, because what they should be doing is having 2 separate versions of each script, one that needs to run on the Platform VS and one that runs on the Portal???

My opinion is irrelevant in this matter

(And I'm asking follow-up questions so that I can have a better understanding. I only have the solution for the very specific question that I asked, but I still don't have a good understanding of how ServiceNow is expecting people to do things. It seems like they don't guide people in the right direction most of the time. But maybe it's me.)

Of course I'm not a spokesperson of ServiceNow, but in my experience it expects one to achieve stuff by sticking to OOB solutions, even if that means "downgrading"/limiting expectations/outcomes

-O-
Kilo Patron

I mean if this is related to the currency formatting library you mention some other place, and entering currency values is the goal, I would consider a two column layout where one side contains the currency drop-down the other side a RegExp validated Single Line Text for the amount.

Even if it seems cumbersome or if it doesn't look exactly as my heart desires.

Going for "clean" is OK, but - in my opinion - one should also go for the "correct" clean.