Need Help Restricting View Based on Role or Group

bishalsharm
ServiceNow Employee
ServiceNow Employee

Hi Community,

 

I'm trying to restrict access to a custom view called "edit" on a specific table.


The requirement is:

Only show the "edit" view to users who:

  • Have the role "xyz"
    OR
  • Belong to the group "abc"

If the user does not meet either of these conditions, they should only see the default view, and the "edit" view should not be selectable or accessible.

 

Any help or guidance is appreciated!

 

Thanks,
Bishal Sharma

3 REPLIES 3

Astik Thombare
Tera Sage

Hi @bishalsharm ,

 

This can be achieved with the help of View Rules in ServiceNow

 

I have created one view with the name "Edit "

 

AstikThombare_0-1753850856366.png

 

Use case would be only 'itil' or users who are part of "Analytics Settings Managers" should able to see 'Edit' View and for other users the view should be 'default'

 

For this I have created View rule as below

 

System UI -> View Rules 

 

AstikThombare_1-1753850965763.png

 

 

AstikThombare_2-1753851014303.png

 

Below is the script 

 

(function overrideView(view, is_list) {


    if (gs.hasRole('itil') || gs.getUser().isMemberOf('Analytics Settings Managers')) {
        answer = 'Edit';
    } else {
        answer = 'default';
    }

})(view, is_list);

 

Let's test this 

 

I have impersonate user "Abraham Lincoln" who is part of "Analytics Settings Managers" group .

 

AstikThombare_3-1753851123163.png

 

After impersonation . the user should able to see 'Edit' View 

 

AstikThombare_4-1753851194054.png

 

Note : to get backend name of view you can navigate to 'sys_ui_view' table 

 

If my answer helped you, please consider marking it as Correct and giving it a 👍 Helpful — it might help other members in the community too!

Thanks,
Astik T 😊💡

 

hi,

 

woulld it be possible to show both default and edit to users who all are either having the role or part of the group and to show only default who fails the condition ?

Ankur Bawiskar
Tera Patron
Tera Patron

@bishalsharm 

you can use view rules for this.

What did you start with and where are you stuck?

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader