Get a first look at what's coming. The Developer Passport Australia Release Preview kicks off March 12. Dive in! 

How to restrict the view based on role

JK9903
Mega Guru

Hello everyone,

I would like to restrict users from changing the view in the RITM and sc_task tables. Specifically, if a user has the "xyz" role, the view should always remain as "demo view," and they should not be able to switch to any other view.

 

I have already created a view rule for this requirement, but users are still able to change the view. Is there a way to enforce the view so that they are always restricted to "demo view"?

Thanks,
JK
4 REPLIES 4

Dr Atul G- LNG
Tera Patron

Hi @JK9903 

 

https://www.servicenow.com/community/developer-forum/restrict-views-based-on-role/m-p/1718098

*************************************************************************************************************
Regards
Dr. Atul G. - Learn N Grow Together
ServiceNow Techno - Functional Trainer
LinkedIn: https://www.linkedin.com/in/dratulgrover
YouTube: https://www.youtube.com/@LearnNGrowTogetherwithAtulG
Topmate: https://topmate.io/dratulgrover [ Connect for 1-1 Session]

****************************************************************************************************************

maliksneha9
Mega Sage

To strictly enforce the view, you’ll need to go beyond view rules. Here are the practical approaches. You need server-side + client-side control for full restriction

 

1. Use Before Query Business Rule

(function executeRule(current, previous /*null when async*/) {

if (gs.hasRole('xyz')) {
gs.getSession().putProperty('sysparm_view', 'demo_view');
}

})();

 

2.  Client Script (Stronger UI Control)

function onLoad() {
if (g_user.hasRole('xyz')) {
var currentView = g_form.getViewName();
if (currentView !== 'demo_view') {
var url = new GlideURL(window.location.href);
url.addParam('sysparm_view', 'demo_view');
window.location.href = url.toString();
}
}
}

 

Hi @maliksneha9 ,

 

I tried the script you provided, but it stops working at the GlideURL line. So I attempted to manually append `sysparm_view` to the URL:

 

(var url = '/' + table + '.do?sys_id=' + sysId + '&sysparm_view=demo_view';

window.location.href = url; )

However, this approach doesn’t seem to work either.

 

Thanks,
JK

yashkamde
Mega Sage

Hello @JK9903 ,

 

For this you will need to create view rule to prevent view access.

Navigate to System UI > View Rules :

 

create a record and in advanced write the script according to your condition.

Screenshot 2026-03-17 175336.png

 

If my response helped mark as helpful and accept the solution.