How to make multiple fields read only from list view for a particular role?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-10-2024 01:24 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-10-2024 01:37 AM
Hello,
If the multiple fields are limited in number, then creating #of list_edit ACLs is a good option with role = 'xyz' and script: answer=false
Alternatively, you can also restrict all fields editing using List Control (right click > Configure) in list view > list edit type: Disable list editing
If my response proves useful, please mark it "Accept as Solution" and "Helpful". This action benefits both the community and me.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-10-2024 01:37 AM
Hi @salonisuman
To make multiple fields read-only in a list view for a particular role in ServiceNow, you can leverage UI Policies or Client Scripts. However, since you're targeting a list view, it's more effective to use List Layouts and Field Level Security (ACLs) to control access to fields. Below are two methods to achieve this:
Method 1: Use Access Control Rules (ACLs) for Field-Level Security
You can create read-only ACLs for specific fields based on a user's role.
-
Navigate to the Access Control List:
- Go to System Security > Access Control (ACL).
- Click on New to create a new access control rule.
-
Set the Table and Field:
- In the Table field, select the table that contains the fields you want to make read-only.
- In the Type field, select Field.
- In the Operation field, select Read (for making the fields read-only).
- In the Field field, specify the field that you want to make read-only.
-
Add Conditions for the Role:
- Under Requires role, add the role(s) for which this rule will apply. Users with these roles will only have read-only access to the specified field.
- Optionally, add further conditions in the Condition script field, such as: current.role == 'specific_role';
-
Repeat for Each Field:
- Repeat the process for each field you want to make read-only in the list view.
Method 2: Use Client Scripts to Set Fields as Read-Only
You can use a client script to target multiple fields in the list view and make them read-only for users with a specific role.
-
Navigate to Client Scripts:
- Go to System Definition > Client Scripts.
- Click New to create a new client script.
-
Configure the Script:
- Set the Table to the appropriate table.
- In the Type field, choose onLoad.
- In the Script section, add the following code:
(function() {
// Check if the user has the specified role
if (g_user.hasRole('specific_role')) {
// List of fields to make read-only
var readOnlyFields = ['field1', 'field2', 'field3'];
// Loop through each field and set it to read-only
for (var i = 0; i < readOnlyFields.length; i++) {
g_form.setReadonly(readOnlyFields[i], true);
}
}
})(); - Replace
'specific_role'
with the role you want to target, and update'field1', 'field2', 'field3'
with the actual field names you want to make read-only.
-
Save and Test:
- After saving the client script, users with the specified role will find the targeted fields read-only when viewing them in the list.
Method 3: Modify List Layouts and Field Configuration
Another approach is to modify the List Layout and hide fields from being edited in the list view, while still allowing them to be visible.
-
Navigate to the List Layout:
- Go to System UI > List Layouts.
- Select the table that contains the fields you want to modify.
-
Set the Fields as Read-Only (via ACLs or List Configuration):
- Configure the list to include the fields you want but restrict edit permissions using the methods above (like ACLs).
Final Considerations:
- ACLs are generally preferred as they work at a deeper security level, ensuring users with specific roles can only view fields in read-only mode, even outside the list view.
- Client scripts are good for more dynamic, UI-level controls but should be used with caution since they can be bypassed through other channels (like APIs).
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
********************************************************************************************************
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-10-2024 02:20 AM
Hi,
Thank you so much for the solutions, really appreciate. I was somehow trying to avoid ACLs but seems there's no other way.
I tried with the client script but it is not working