How to restrict form views based on role?

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-24-2021 02:48 AM
Hi,
Consider we have 3 views as below
view 1
view 2
view 3
Above 3 views are visible to admin and itil users. But I want to restrict in such a way that, itil users should see only view 1 and view 2, not view 3.
Can anyone suggest how we can achieve this. Thank you!
NOTE: I tried adding roles to System UI -> Views -> on Role field but it didn't work.
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-24-2021 02:51 AM
Hi,
You can create View Rule
Control when the system displays a view | ServiceNow Docs
-Anurag

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-24-2021 02:53 AM
Hello Anurag,
I tried that with below script
i,e
if(gs.hasRole('itil'))){
answer = 'view 1' }
But Whenever I do impersonate to ITIL user, form is forcing me to stay in view 1. Its not allowing me to change the view also.
Could you please suggest me sample script to achieve this. Thank you!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-28-2021 02:56 PM
Haven't tried this myself but how about something like this?
var view = g_form.getViewName();
if (gs.hasRole('itil') && view == 'view 3') {
//Only revert to view 1 as a default if view 3 is selected
//otherwise allow user to select view
answer = 'view 1';
}
The opinions expressed here are the opinions of the author, and are not endorsed by ServiceNow or any other employer, company, or entity.