Set list / form view by role

Shane11
Mega Expert

Hi everyone,

What I am trying to accomplish :
When a user logs in with a specific role which doesn't have the ability to change views I want to set which view they see based on their role.
Specifically we want to give our SaaS team the ability to view incidents using a specific view created for them so they see only the information they require and we can modify it to suit their needs.
I created the view and assigned it the SaaS role, which I'm thinking is who can see the view and not which role uses that view.

What happens :
When I log in using my test account which has the SaaS role it uses the ess view.
I can give them the ability to change views and use the roles to lock them out of the ones I don't want them to get to, I would still want the SaaS view to be their default, but I would prefer that they can't switch views and are forced to use the SaaS view.
(though I will follow best practices if it speaks against doing it that way)

I haven't been able to find the answer in the wiki or forums and haven't found enough documentation to figure it out myself.
I hope someone out there can help me out.
Thank you in advance.

-Shane

2 REPLIES 2

zachkenyon
ServiceNow Employee
ServiceNow Employee

If you look at the OOB business rule "incident functions" there is code in there for the Incident table that does this.

Essentially, you'd need to make a global Business Rule and add similar functions to it for the tables you want to control the view for. the functions need to be named: table_nameGetViewName() and table_name_listGetViewName(). Set the "answer" variable in the function to the name of the view you want the user to have.

This example will control the views for the table u_saas, setting users with the role "saas" to the view called "saas", setting users with no role to the "ess" role, and leaving other roled users to the default role:

function u_saasGetViewName() {
if (gs.hasRole('admin')) {
return;
}
if (gs.hasRole('saas')) {
answer = "saas";
return;
}
if (!gs.getUser().hasRoles()) {
answer = "ess";
return;
}
answer = "";
}

function u_saas_listGetViewName() {
if (gs.hasRole('admin')) {
return;
}
if (gs.hasRole('saas')) {
answer = "saas";
return;
}
if (!gs.getUser().hasRoles()) {
answer = "ess";
return;
}
answer = "";
}


Thank you so much!
I've been bashing my head against a wall, that worked perfectly and will be very useful in the future!