The CreatorCon Call for Content is officially open! Get started here.

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

2 REPLIES 2

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...

 

 

If you found this response helpful, please mark it as Helpful. If it fully answered your question, consider marking it as Correct. Doing so helps other users find accurate and useful information more easily.

kaushal_snow
Mega Sage

Hi @vivek11 ,

 

You can implement this using either a Before Query Business Rule or an Access Control List (ACL).

A Before Query Business Rule allows you to filter records at the database query level, ensuring that users only retrieve records they are authorized to see. This method is efficient as it prevents unauthorized records from being loaded in the first place.

 

Alternatively, you can use an Access Control List (ACL) to restrict access to records based on the user's department. An ACL provides a more granular level of security, allowing you to control access to records based on specific conditions...

 

If you found my response helpful, please mark it as ‘Accept as Solution’ and ‘Helpful’. This helps other community members find the right answer more easily and supports the community.

 

Thanks and Regards,
Kaushal Kumar Jha - ServiceNow Consultant - Lets connect on Linkedin: https://www.linkedin.com/in/kaushalkrjha/