Redirect snc_internal users to the Service Portal in ServiceNow
Below we highlight a method for redirecting users with only a "snc_internal" user role to the Service Portal, as out of the box users with Any role get directed to the UI 16 back end.
Start by opening up the Script Include SPEntryPage
Next, find the function getFirstPageURL and modify it similarly to below, you will need to find the Sys ID of the snc_internal role prior to doing this step.
getFirstPageURL: function() {
var session = gs.getSession();
this.logProperties('before', session);
// has roles and is not a Service Portal page - go to UI16
var nt = session.getProperty("nav_to");
var isServicePortalURL = new GlideSPScriptable().isServicePortalURL(nt);
var redirectURL = session.getProperty("login_redirect");
// NEW - Check for users with only snc_internal role
var SNCInternalUserRole = "debab85bff02110053ccffffffffffb6"; // Sys id of snc_internal role must be put here
var SNCInternalRole = new GlideRecord("sys_user_has_role");
SNCInternalRole.addQuery("user",gs.getUserID());
var roleCount = 0;
var hasSNCInternalRole = false;
SNCInternalRole.query();
while(SNCInternalRole.next()){
roleCount++;
if(SNCInternalRole.getValue("role") == SNCInternalUserRole){
hasSNCInternalRole = true;
}
}
var hasSNCInternalRoleOnly = (roleCount == 1 && hasSNCInternalRole);
//
if (user.hasRoles() && !hasApproverRoleOnly && !redirectURL && !isServicePortalURL)
return;
// user may have logged in from a frame, the /login_redirect.do page will bust out of it
if (!redirectURL) {
// redirectURL is nav_to
// if nav_to == "welcome.do" then use starting_page
var sPage = session.getProperty("starting_page");
if (sPage && nt == "welcome.do")
nt = sPage;
// Avoid a redirect loop to the home page
var ep = gs.getProperty("glide.login.home");
if (nt) {
if (ep == nt)
nt = null;
}
// PRB726860: if page is still welcome.do, go to glide.login.home preserving frameset
if (nt == "welcome.do") {
session.putProperty("nav_to", ep);
return;
}
session.putProperty("login_redirect", nt || "true");
return "/login_redirect.do?sysparm_stack=no";
}
session.clearProperty("login_redirect");
var returnUrl = this.portal;
if (redirectURL && redirectURL != "true") {
var spUrl = new GlideSPScriptable().mapUrlToSPUrl(redirectURL);
returnUrl = spUrl ? this.portal + "?" + spUrl : redirectURL;
}
this.logProperties('after', session);
if (!this.logVariables) {
gs.log('redirectURL: ' + redirectURL);
gs.log('User: ' + user.getName());
gs.log('is internal: ' + (!user.hasRoles()));
gs.log('returnUrl: ' + returnUrl);
}
return returnUrl;
},
That's it! All users with no roles, or only the snc_internal role, should now get redirected to the Service Portal.
Comments ()