Change table

ServiceNow40
Tera Contributor

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.

 

1 REPLY 1

PrashantLearnIT
Giga Sage

Hi @ServiceNow40 

 

Option 1: Using a UI Policy

  1. Navigate to System UI > UI Policies.

  2. Create a new UI Policy.

  3. 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)
  4. 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.

  1. Navigate to System UI > Client Scripts.

  2. Create a new Client Script.

  3. Set the following fields:

    • Table: Change Request [change_request]
    • Type: onLoad
  4. 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 using style.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.

  1. Navigate to System Security > Access Control (ACL).
  2. 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
********************************************************************************************************