Community Alums
Not applicable

Hello Community,

 

I was working on a client project and faced a challenge in redirecting users to the Customer Service Management (CSM) portal based on their role. After digging into the problem, I finally found a solution. I noticed that many users face the same issue, so to help others, I'm sharing this quick and simple guide.

Solution Overview

Our goal is to redirect users with the role to the CSM portal automatically. This ensures the security and integrity.

 

Step 1: Creating the client callable Script Include
First, we need a server-side Script Include to check if a user has a specific role:

 

var UserRoleCheck = Class.create();
UserRoleCheck.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    userHasRole: function() {
        var role = this.getParameter('role'); // Get the role from the client-side request
        //gs.log('P1:CSM - User: '+gs.getUser().getName()+' Checking role: ' + role);
        var user = gs.getUser();
        var hasRole = user.hasRole(role);
  
        //gs.log('P1:CSM - User: '+gs.getUser().getName()+' || Checking role: ' + role +' || Has Role: '+hasRole);
        if(user.hasRole('admin')){
            return 'false';
        }
        return JSON.stringify(hasRole);
    },
    type: 'UserRoleCheck'
});

 

 

Step 2: Creating a UI Script

Next, create a client-side UI Script to call the Script Include using GlideAjax and redirect users:

 

function checkUserRole(role) {
    var ga = new GlideAjax('UserRoleCheck');
    ga.addParam('sysparm_name', 'userHasRole');
    ga.addParam('role', role);
    ga.getXMLAnswer(function(response) {
        if (JSON.parse(response) === true) {
            window.location.href = '/csm';
        }
    });
}

if (document.URL.indexOf('.do') !== -1) {
    checkUserRole('sn_customerservice.customer');
}

 

Note: Replace the role as per your requirement.

 

Testing
1. Clear your browser cache.

2. Navigate to https://<<INSTANCE NAME>>.service-now.com/<<table_name>>_list.do
3. Verify the redirection to /csm if the user has the sn_customerservice.customer role.

 

Additional Information
The UI script mentioned above is just an idea of how you can approach to mitigate this behavior.
Further customization will be out of the scope of support for the technical support department.

Some additional custom validation ideas that you can try in the UI script to check if the URL is a portal URL or not:
- Portal URLs will always have 'id='
- Portal URLs do not have '.do' or '_list.do'
- Use GlideAjax within the UI script to add more server-side validations such as members of groups, companies, etc. Please keep in mind that server-side queries can have a performance impact on the instance. On how to use GlideAjax in UI script.

 

Please mark it as helpful and accept it as a solution if it helps you in any way.

Regards,
Prasad - "Here to help and empower."

 

Version history
Last update:
‎05-29-2024 12:31 PM
Updated by:
Community Alums
Contributors