need to give the permission to users based on view

siva14
Tera Contributor

Hello all,

 

i have small requirement that , on custom table list view i have created a new view name called "XXX" , so i need to give list edit permission to the users to edit the fields on list view when they are on view called "XXX", 

i have used below script but the script is not working , any suggestion that would be great help.

Note - I have created new role to those users.

 

(function() {
    var currentView = gs.getSession().getClientData('sysparm_view');
    if (currentView === 'XXX') {
        answer = true;
    } else {
        answer = false;
    }
})();
 
#ACL #ACL SCRIPT #SECURITY ADMIN
1 ACCEPTED SOLUTION

folusho
Tera Guru

@siva14 

Please try this script below:

 

/* Restrict to view = “XXX” */
(function () {

    var view = 'default';

    // When a browser call is in play, get the ?sysparm_view=value
    var req = gs.action.getGlideRequest();   // null if no request
    if (req) {
        var v = req.getParameter('sysparm_view');
        if (v) {
            view = v;
        }
    }

    // Only allow if the requested view is “XXX”
    answer = (view === 'XXX');
})();

 

Why switch from gs.getSession().getClientData('sysparm_view')?

  1. gs.action.getGlideRequest() is the supported server‑side way to read URL parameters such as sysparm_view.

  2. getClientData() relies on UI‑only session data... it isn’t always populated when the script runs in background or via API.

View solution in original post

7 REPLIES 7

_ukasz Rybicki
Giga Guru

🛠️ Problem Name: List Edit Restriction by View


🔍 General Solution Proposal:

To restrict list editing on a custom table view named "XXX", implement a list_edit ACL with a script that checks the current view. This ensures that only users accessing the "XXX" view can edit fields inline, while others are restricted.


📝 Detailed Step-by-Step Solution:

  1. Elevate Roles:

    • Navigate to your profile and select Elevate Roles.

    • Check security_admin and submit.

  2. Create a New ACL:

    • Go to System Security > Access Control (ACL).

    • Click New to create a new ACL.

  3. Configure ACL Details:

    • Type: Record

    • Operation: list_edit

    • Name: Select your custom table and the specific field to restrict.

    • Active: Checked

    • Admin Overrides: Unchecked (if you want to enforce this restriction for admins as well)

    • Requires Role: Add the role(s) that should have edit permissions in the "XXX" view.

  4. Advanced Script:

    • Check the Advanced box.

    • Enter the following script:

      (function() {
          var view = gs.action.getGlideURI().get('sysparm_view');
          if (view === 'XXX') {
              answer = true;
          } else {
              answer = false;
          }
      })();
    • This script checks the current view. If it's "XXX", it allows list editing; otherwise, it denies it.

  5. Save the ACL:

    • Click Submit to save the ACL.


Example Use Case:

Scenario: You have a custom table u_custom_table with a view named "XXX". Only users with the role u_custom_role should be able to edit the u_custom_field inline when accessing the "XXX" view.

Implementation:

  • Create a list_edit ACL for u_custom_table.u_custom_field.

  • Assign the role u_custom_role in the ACL.

  • Use the above script to check for the "XXX" view.


🧪 Testing the Solution:

  1. Impersonate a User:

    • Impersonate a user with the appropriate role.

  2. Access the "XXX" View:

    • Navigate to the list view of your custom table.

    • Ensure you're in the "XXX" view.

  3. Test Inline Editing:

    • Attempt to edit the field inline.

    • It should be editable.

  4. Switch Views:

    • Change to a different view.

    • Attempt to edit the field inline.

    • It should be read-only.


📚 Sources:

  1. ServiceNow Community - ACL to restrict users to edit fields from list view:

    • Link

    • Discusses creating list_edit ACLs to control inline editing based on roles and views.

  2. ServiceNow Community - Get view name from list control omit new condition script:

    • Link

    • Provides insights on retrieving the current view using gs.action.getGlideURI().get('sysparm_view').


If this solution works for you, please mark it as the accepted answer. Let me know if you need further assistance! 😊


folusho
Tera Guru

@siva14 

Please try this script below:

 

/* Restrict to view = “XXX” */
(function () {

    var view = 'default';

    // When a browser call is in play, get the ?sysparm_view=value
    var req = gs.action.getGlideRequest();   // null if no request
    if (req) {
        var v = req.getParameter('sysparm_view');
        if (v) {
            view = v;
        }
    }

    // Only allow if the requested view is “XXX”
    answer = (view === 'XXX');
})();

 

Why switch from gs.getSession().getClientData('sysparm_view')?

  1. gs.action.getGlideRequest() is the supported server‑side way to read URL parameters such as sysparm_view.

  2. getClientData() relies on UI‑only session data... it isn’t always populated when the script runs in background or via API.

siva14
Tera Contributor

thanks @folusho , it's working as expected.

siva14
Tera Contributor

Hi @folusho ,

 

i found issue that, it allowing to edit the fields in both default and XXX views, how to allow only in XXX view?