How to restrict the view based on role
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 hours ago
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"?
JK
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 hours ago
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]
****************************************************************************************************************
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 hours ago
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();
}
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 hours ago
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.
JK
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
2 hours ago
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.
If my response helped mark as helpful and accept the solution.
