The Zurich release has arrived! Interested in new features and functionalities? Click here for more

How to Restrict Access to test Table Using Before Query Business Rule or ACL Based on User Departme

vivek11
Tera Contributor

Hi everyone,

I am working on restricting access to the test table so that only users belonging to the it or csa Department can view the data.

Requirements:
Users from it or csa Department should be able to see and interact with the records normally.
Users from other Departments should not be able to see any records.
I want to implement this using ACL or Before qurry business rule:

 

Can someone please help me in code, If possible provide with screenshot and link.

Thank you,
Vivek

1 REPLY 1

Rafael Batistot
Kilo Patron

Hi @vivek11 

 

If you really want to hide records entirely from users in other departments, you can filter the query before it runs.

 

1. Go to Business Rules > New.

 

2. Set:

  • Table: your table (u_test)
  • When: before
  • Insert/Update/Delete: unchecked
  • Query: checked

3. Script:

 

(function executeRule(current, previous /*null when async*/) {

var userDept = gs.getUser().getDepartmentID();
if (!userDept) {
current.addQuery('sys_id', ''); // no records
return;
}

var dept = new GlideRecord('cmn_department');
if (dept.get(userDept)) {
if (dept.name != 'it' && dept.name != 'csa') {
// Exclude all records for users outside IT or CSA
current.addQuery('sys_id', '');
}
}

})(current, previous);

 

https://www.servicenow.com/community/developer-blog/query-business-rules-a-definitive-guide/ba-p/227...