Service Portal - Redirect via UI Script
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-20-2017 01:08 PM
This is part of a UI Script that redirects a user based on roles. Line 21 isn't being called because window.localStorage isn't coming back as null (this seems to only happen if the user has a specific role).
I logged the window.localStorage in another instance that is still using CMS (and an older version of this UI Script) and it came back as null. Any ideas???
addLoadEvent(function() {
try{
if ( typeof g_user.userName != "undefined" && g_user.userName == 'guest' ) {
window.localStorage.removeItem('evgRedirectLaunched');
console.log('[RedirectHandler] Removed evgRedirectLaunched key, on login page');
}
if ( typeof g_user != "undefined") {
if ( typeof g_user.allRoles != "undefined") {
if ( g_user.allRoles.indexOf('ess_it_user') == -1 && g_user.allRoles.indexOf('admin') == -1 ) {
window.localStorage.removeItem('evgRedirectLaunched');
console.log('[RedirectHandler] Removed evgRedirectLaunched key, user is ESS user');
}
}
}
var evgDirectLaunched = window.localStorage.getItem('evgRedirectLaunched');
console.log('evglaunch: '+evgDirectLaunched);
if ( evgDirectLaunched == null ) {
console.log('[RedirectHandler] evgRedirectLaunched NOT set, processing launchredirect, g_user.userName = '+g_user.userName);
launchredirect();
}
} catch (err) {
console.log('[RedirectHandler] Error: ' + err);
}
});

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-20-2017 03:49 PM
on a side note, there is a better way to do it rather than a UI Script: SPEntryPage Script Include.
Have a read:
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-20-2017 04:47 PM
We have 3 different portals so the SPentrypage wasn't working for us
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-18-2018 01:01 PM
Sidenote, pretty easy to make it redirect to different portals depending on conditions.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-20-2017 04:01 PM
g_user is a global object in GlideUser (the Javascript class).
g_user
- can only be used in Client Scripts.
- contains name and role information about the current user.
- is typically used in Client Scripts and UI Policies but is also found in UI Actions which run on the client.
- cannot be used in Business Rules or UI Actions which run on the server.
- avoids the need for much slower user queries (for example, GlideRecord queries to get user information)
addLoadEvent(function() {
try {
if (gs.getUserName() == 'guest') {
window.localStorage.removeItem('evgRedirectLaunched');
console.log('[RedirectHandler] Removed evgRedirectLaunched key, on login page');
}
//SURE THIS IS AND???
if (gs.hasRole('ess_it_user') && gs.hasRole('admin')) {
window.localStorage.removeItem('evgRedirectLaunched');
console.log('[RedirectHandler] Removed evgRedirectLaunched key, user is ESS user');
}
var evgDirectLaunched = window.localStorage.getItem('evgRedirectLaunched');
console.log('evglaunch: ' + evgDirectLaunched);
if (evgDirectLaunched == null) {
console.log('[RedirectHandler] evgRedirectLaunched NOT set, processing launchredirect, g_user.userName = ' + g_user.userName);
launchredirect();
}
} catch (err) {
console.log('[RedirectHandler] Error: ' + err);
}
});
If this reply assisted you, please consider marking it ✅Correct, âÂ��Helpful, or ��Liking it.