Change table
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-08-2024 09:23 PM
I need to hide the views for change table to non admin users.
Using script but not working.
var restrictedView = 'service';
if(current.getViewname() == 'restrictedView'){
if(!gs.hasRole('admin')){
answer = false;
} else {
answer = true;
}
}
Any idea help me the proper code.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-08-2024 09:27 PM
Option 1: Using a UI Policy
-
Navigate to System UI > UI Policies.
-
Create a new UI Policy.
-
Set the following fields:
- Table:
Change Request [change_request]
(or whichever table you're using) - Conditions: Add conditions as needed (e.g., to target specific records)
- OnLoad: Set to
true
(checked)
- Table:
-
In the Script section, write a script to hide the "View" dropdown based on the user role.
Here is a sample script:
// This script will run when the form is loaded
if (!gs.hasRole('admin')) {
// Hide the view selector if the user is not an admin
g_form.addClassName('view_name', 'hidden');
}
Option 2: Using a Client Script
If you want more control or want to target specific conditions dynamically, you can also use a Client Script.
-
Navigate to System UI > Client Scripts.
-
Create a new Client Script.
-
Set the following fields:
- Table:
Change Request [change_request]
- Type:
onLoad
- Table:
-
Use this script to hide the views for non-admin users:
function onLoad() {
// Check if the user does not have admin role
if (!g_user.hasRole('admin')) {
// Hide the view selector (UI element)
var viewSelector = document.querySelector('.view-picker-button');
if (viewSelector) {
viewSelector.style.display = 'none';
}
}
}
Explanation:
- gs.hasRole('admin'): Checks if the current user has the
admin
role on the server-side (UI Policy Script). - g_user.hasRole('admin'): Checks the user’s role on the client-side (Client Script).
- view-picker-button: This is the CSS class used for the "Views" dropdown button in the UI. The
querySelector()
method selects the element, and then it is hidden usingstyle.display
.
Option 3: Using ACL (Access Control)
If you want a more secure approach, consider using an ACL rule that controls who can see certain parts of the UI based on roles.
- Navigate to System Security > Access Control (ACL).
- Create a new rule for
change_request
table and use conditions or scripts to deny view access for non-admins.
Would you prefer UI Policy, Client Script, or ACL?
Please appreciate the efforts of community contributors by marking the appropriate response as the correct answer and helpful. This may help other community users to follow the correct solution in the future.
********************************************************************************************************
Cheers,
Prashant Kumar
ServiceNow Technical Architect
Community Profile LinkedIn YouTube Medium TopMate
********************************************************************************************************