UI script to work automatically without being called

Kevin Paul
Mega Guru

Hi,

 

I have written a UI script to check logged in users' role and if user has a specific role only, then he would be redirected to the Portal page. 

(function() {
    if (window.self === window.top) { 
        if (g_user.hasRoleExactly('user')) {
            var currentURL = window.location.href;
            if (currentURL.includes('.do') && !currentURL.includes('/sp') && !currentURL.includes('/nav_to')) {
                window.location.href = '/esc';
            }
        }
    }

Currently When I set the UI type to ALL, the global checkbox is disabled. So I can't run it automatically on all the pages that user tries to enter in the URL. Is there any way to do this? I am looking to restrict the user to the portal only when user tries to provide a deep link in the URL.

 

KevinPaul_0-1756478164723.png

 

Any help is appreciated. Thanks!

7 REPLIES 7

Ankur Bawiskar
Tera Patron
Tera Patron

@Kevin Paul 

ServiceNow restricts Global UI scripts from automatically running on ALL pages due to performance and security reasons.

Why are you not using SPEntryPage Script include and handle logic there?

How to setup portal redirection based on different roles 

If my response helped please mark it correct and close the thread so that it benefits future readers.

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

@Ankur Bawiskar I tried that first but when given deep links (target parameters or the table names directly) the redirection does not happen. 

@Kevin Paul 

I believe the script include approach is the only way forward.

If my response helped please mark it correct and close the thread so that it benefits future readers.

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

Rafael Batistot
Kilo Patron

Hi @Kevin Paul 

To achieve your goal of restricting users to the portal even with deep links, consider these alternatives:
  • This is the recommended and most robust approach for handling entry point redirection based on user roles.
  • Navigate to System Definition > Script Includes and find SPEntryPage.
  • Modify the getFirstPageURL() function to include your role-based redirection logic. You can check for the "user" role and redirect them to /esc (or your specific portal suffix) if they attempt to access non-portal URLs.
  • Example adaptation of SPEntryPage (simplified):Modify the SPEntryPage Script Include:
  • Code:

 

// In the getFirstPageURL function, before the default return statement:
if (gs.getUser().hasRoleExactly('user')) { // Check for the specific role
    var currentURL = gs.action.getURL(); // Get the requested URL
    if (!currentURL.includes('/sp') && !currentURL.includes('/esc') && !currentURL.includes('/nav_to.do')) { // Exclude portal and nav_to links
        return '/esc'; // Redirect to your portal
    }
}
// Existing logic for other users/scenarios