UI script to work automatically without being called
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-29-2025 07:36 AM
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.
Any help is appreciated. Thanks!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-29-2025 07:49 AM
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.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
a month ago
@Ankur Bawiskar I tried that first but when given deep links (target parameters or the table names directly) the redirection does not happen.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
a month ago
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.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-29-2025 09:28 AM - edited 08-29-2025 09:29 AM
Hi @Kevin Paul
- 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