Service Portal - Redirect via UI Script

Blair5
Tera Guru

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);

}

});

12 REPLIES 12

vab_13
ServiceNow Employee
ServiceNow Employee

on a side note, there is a better way to do it rather than a UI Script: SPEntryPage Script Include.


Have a read:


https://docs.servicenow.com/bundle/jakarta-servicenow-platform/page/build/service-portal/concept/c_S...


We have 3 different portals so the SPentrypage wasn't working for us


Sidenote, pretty easy   to make it redirect to different portals depending on conditions.


vrfox
Giga Contributor

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.