Assistance Required with Portal Routing Logic Based on User Company

Gulfam
Giga Guru

Hi Expert

I have a requirement related to portal routing that I need some help with. Here's the scenario:

  • If a user belongs to company "ABC," they should be routed to the Service Portal (SP).
  • If a user belongs to company "XYZ," they should be routed to the Customer Service Management portal (CSM).
  • After logging out, the user should be redirected to the common login page (SP landing page).

I’ve created a new Script Include that inherits the SPEntryPage Script Include. In the initialize function, I added the following code:

 

 
var userId = gs.getUserID();
var userRec = new GlideRecord('sys_user'); userRec.addEncodedQuery('sys_class_name=customer_contact^company.nameINXYZ^sys_id=' + userId); userRec.query();
if (userRec.next()) {
this.portal = "/csm/";
} else {
this.portal = "/sp/";
}
 

When I impersonate a user from company "XYZ," the CSM portal is displayed as expected. However, when I log in using my credentials (from company "XYZ"), it always redirects me to the Service Portal (SP) instead of the CSM portal.

Could you please assist me in resolving this? I suspect there may be an issue with how the user's company is being checked or how the logic is being applied during login.

Looking forward to your guidance on this.

12 REPLIES 12

@Gulfam 

when you impersonated with user from XYZ what came in log?

when you impersonated with user from another company what came in log?

 

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

Dimah_0-1734971653861.png

 

I have added following logs, 
When i tried with credentials following code triggered thrice. 2 times for csm user and 1 times for guest user.
But when i impersonate, same code trigger twice. and both times for CSM user


 

Runjay Patel
Giga Sage

Hi @Gulfam ,

 

Your code looks okay, you need to check routing. try printing the log to troubleshoot.

RunjayPatel_0-1734962327860.png

 

-------------------------------------------------------------------------

If you found my response helpful, please consider selecting "Accept as Solution" and marking it as "Helpful." This not only supports me but also benefits the community.


Regards
Runjay Patel - ServiceNow Solution Architect
YouTube: https://www.youtube.com/@RunjayP
LinkedIn: https://www.linkedin.com/in/runjay

-------------------------------------------------------------------------

 

Hi Runjay,
I have write the logs, it's showing csm portal in logs, When i impersonate with XYZ user then it takes me to the CSM portal and when i try to log in using XYZ user then it take me to the SP portal.

Kieran Anson
Kilo Patron

Your encoded query could be troublesome with doing a contains search. You're also limiting the search to only users with a class of contact. 

 

Are you only wanting this to apply to a single company record? Or are there actually many companies this needs to work for.