Redirect user to KB article
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-11-2024 12:26 AM
Hi,
I want to redirect user to a KB article when the go to supplier portal upon certain conditions are matched. below script not working which I added in SPEntryPage script include.
if(user.hasRole("snc_internal") && user.getValue("u_user_type") == "Guest" && user.getValue("u_org_id") == "")
{
returnUrl = "https://supplierportal.asda.uk/supplier?id=kb_article_view&sysparm_article=KB0034151";
return returnUrl;
}
Full Code
var SPEntryPage = Class.create();
SPEntryPage.prototype = {
initialize: function() {
this.logVariables = false; // for debugging
this.portal = this.getDefaultPortal(); // The URL suffix for default portal
},
/***
*
* Fetch the default portal suffix
*
**/
getDefaultPortal: function() {
var gr = new GlideRecord("sp_portal");
gr.addQuery("default", true);
gr.query();
if (gr.next())
return "/" + gr.getValue("url_suffix") + "/";
return "/esc/";
},
/***
*
* Referred to by property: glide.entry.page.script
*
**/
getLoginURL: function() {
// When requesting a page directly (eg: /problem_list.do)
// The platform session_timeout.do sets the login page to welcome.do
// Since we are handling the login, clear that value
var session = gs.getSession();
var nt = session.getProperty("nav_to");
var sPage = session.getProperty("starting_page");
if (nt == "welcome.do")
session.clearProperty("nav_to");
if (!sPage && !nt)
session.putProperty("starting_page", gs.getProperty("glide.login.home"));
// Send the user to the portal's login page
var portalGR = new GlideRecord("sp_portal");
portalGR.addQuery("url_suffix", this.portal.replace(/\//g, ""));
portalGR.addNotNullQuery("login_page");
portalGR.query();
if (portalGR.next())
return this.portal + "?id=" + portalGR.login_page.id;
// Send to the a default login page
return this.portal + "?id=login";
},
/***
*
* Referred to by property: glide.entry.first.page.script
*
**/
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");
if (user.hasRoles() && !redirectURL && !isServicePortalURL)
return;
//Not working
if(user.hasRole("snc_internal") && user.getValue("u_user_type") == "Guest" && user.getValue("u_org_id") == "")
{
gs.addInfoMessage("Test");
returnUrl = "https://supplierportal.asda.uk/supplier?id=kb_article_view&sysparm_article=KB0034151";
return returnUrl;
}
//Not working
// 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");
session.clearProperty("nav_to");
if (redirectURL && redirectURL.indexOf("sys_attachment.do") > -1)
return redirectURL;
var returnUrl = this.portal;
if (redirectURL && redirectURL != "true") {
var spUrl = new GlideSPScriptable().mapUrlToSPUrl(redirectURL);
returnUrl = spUrl ? this.portal + "?" + spUrl : redirectURL;
if (!user.hasRoles() && !spUrl && redirectURL.indexOf("home_splash.do") > -1)
returnUrl = this.portal;
}
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);
}
if(user.hasRole("snc_external") && user.hasRole("sn_customerservice.customer") && !user.hasRole("admin"))
{
returnUrl = "https://asdadev1.service-now.com/supplier";
return returnUrl;
}
return returnUrl;
},
logProperties: function(beforeOrAfter, session) {
if (!this.logVariables)
return;
gs.log('SPEntryPage: Redirect ------------------------------- ' + beforeOrAfter);
gs.log('session.starting_page: ' + session.getProperty("starting_page"));
gs.log('session.nav_to: ' + session.getProperty("nav_to"));
gs.log('session.login_redirect: ' + session.getProperty("login_redirect"));
gs.log('gs.fURI: ' + session.getURI());
},
type: 'SPEntryPage'
};
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-11-2024 02:03 AM - edited 09-11-2024 02:03 AM
Hi @Rosy14 ,
User the below code for the problematic section
// Getting loggedin user details
var user = gs.getUser();
var roleCheck = user.hasRole("snc_internal");
var userGr = user.getRecord();
// Performing the checks
if (roleCheck && (userGr.getValue("u_user_type") == "Guest") && (!userGr.getValue("u_org_id"))) {
returnUrl = "https://supplierportal.asda.uk/supplier?id=kb_article_view&sysparm_article=KB0034151";
return returnUrl;
}
If this solution helps you then, mark it as accepted solution ✔️ and give thumbs up 👍!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-11-2024 02:12 AM
The user should redirect if the url contain supplier.. this script is not working properly. After login the user is redirecting here but if he goto supplier portal it can access the page but it should not