Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Redirect ITIL users to CMS

Blair5
Tera Guru

We already have login rules in place to redirect non-roled users to our CMS. However, we would like to route all users to the CMS -- how? Also, for itil roled users, we would like to have them click on a link from within the CMS and be routed back to the itil view.

 

For non-roled users, we have a homepage defined for them with a content block that redirects them to the CMS site. We don't want to do that for itil people because we don't want them to be rerouted back to CMS if this click on the link to go to the itil site. Any suggestions?

1 ACCEPTED SOLUTION

Excellent point, yes, if the ITIL user changes their home page, they would bypass your script.   A better alternative is to create it as a global UI Script.   Try this one out:



UI Script



Name: ESS Redirect


Active: True


Description: Redirects users to the ESS site


Global: True


addLoadEvent(function() {


        if (g_user.hasRole('admin') {


                  // Do nothing if user is admin, this is to allow testing of the script without risk


        }


        // If user has no roles and they are on a non ESS page


        else if (!g_user.hasRoles() && document.URL.indexOf('ess') == -1) {


                  // Redirect to ESS


                  window.location = 'https://instancename.service-now.com/ess';


        }


        // If user has roles, they are on a non ESS page and they were not referred from the ess site


        else if (g_user.hasRoles() && document.URL.indexOf('ess') == -1 && top.document.referrer.indexOf('ess') == -1) {


                  // Redirect to ESS


                  window.location = 'https://instancename.service-now.com/ess';


        }


});



NOTE:   Global UI scripts will run on both ITIL site and ESS site.   This is why the document.URL.indexOf('ess') is necessary, it prevents redirection from the ESS site to the Ess site.


View solution in original post

63 REPLIES 63

poojasingh
Giga Contributor

Hi Travis,



We tried using thee If Condition, but its still not working.


Hi Pooja,



I did a little more digging and found my notes on the redirect!   Here is the latest variant that I have used:



addLoadEvent(function() {


  if (g_user.hasRoles()) return;


  if (top.document.URL.indexOf('ess') > -1) return;


  if (document.URL.indexOf('navpage') > -1) return;


  if (document.URL.indexOf('welcome') > -1) return;




  top.window.location = 'ess';


});



Basically, after the page loads, the script will redirect unless any of the "if" conditions are met.   You can easily extend those conditions or modify them as needed.   I don't recall exactly why I included navpage and welcome in the exemptions but I believe those were included due to issues with the login page.



Kind regards,



Travis Toulson


Henk5
Tera Contributor

HI Travis,



What version did you use this on? Testing it on Helsinki Patch 4 at the moment. I like your clean and simple coding, it almost works just an issue with navpage.do.



If a user logs on to URL https://xxx.service-now.com al works fine but if you log out, you get redirect to https://xxx.service-now.com/navpage.do from that page everybody (even admins) get redirect to ess, actually the opposite of what I would expect.



I can't put my finger on it (yet). Any ideas?



Regard,


Henk


Henk5
Tera Contributor

Ok, my first analysis wasn't entirely correct. It's not the navpage.do that makes the difference. It's changing from an ess user to a normal user that's not working.



- Login with user without roles --> redirect to /ess 🙂


- Logout--> redirected to /ess/main.do


- Remove the /ess part of the url --> normal login page


- Login with user with roles --> redirected to /ess 😞



Somehow the ess user's session leaves something behind. It's not very likely an ESS and Itil user will use the same pc let alone same browser session. But still would like to understand what's causing this.



Any advice on how the improve the ui script?


Hi Henk,



Interesting, I'll have to give that a shot and see if I can replicate it.   The hardest part of the script is that because its a global UI Script, it executes on EVERY SINGLE PAGE.   That includes any "pages" that are merely redirect pages and ServiceNow loves them some "window.location = 'some_other_page.do';".



The most likely cause is that somewhere in the redirects g_user either doesn't exist or the user is not considered logged in yet so the g_user test is false.   The navpage and welcome page checks are two examples where the g_user check will fail, so I called them out explicitly.



Two things you can do to check:



1.   Watch for any JS errors in the console and make sure you persist the console so it doesn't clear every time the page reloads (your browser dev tools should have an option for this somewhere)



2.   With the persist log set, use the network timelines in your browser dev tools to see what pages you are landing on in between clicking login and landing on the ESS.   It can give you some clues as to which page may be causing the issue.