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

Ravi Gaurav
Giga Sage
Giga Sage

Before Query Business Rule

 

Before query Business rule is a special type of business rule in ServiceNow that is used when we want to limit which records can users access from a given table.

This type of business rule runs before a query is executed and can modify the query conditions to filter data based on domain or other criteria.

 

Use Case :-

current logged in user can only see the incident record (List View) which is assigned
to a assignment group to whom he is part of.

 

Business Rule :-

if (gs.getUser().isMemberOf('6df8cc46474d02901279750cd36d43e4') && gs.getSession().isInteractive())

{

// If the user is a member of the group and the session is interactive, proceed current.addQuery('assignment_group', '6df8cc46474d02901279750cd36d43e4');

}

else {

// If the conditions are not met (user is not in the group or session is not interactive), return without further action return;

}

Note :- 

  1. gs.getUser(): This retrieves the current user object from the GlideSystem (gs) API. The gs.getUser() method is used to get information about the currently logged-in user.

  2. .isMemberOf('6df8cc46474d02901279750cd36d43e4'): This checks if the current user is a member of a specific group identified by the provided group ID ('6df8cc46474d02901279750cd36d43e4'). The method .isMemberOf() returns true if the user belongs to the specified group; otherwise, it returns false.

  3. &&: This is the logical AND operator, which combines two conditions. In this case, it connects the previous condition (gs.getUser().isMemberOf('6df8cc46474d02901279750cd36d43e4')) with the next condition (gs.getSession().isInteractive()).

  4. gs.getSession().isInteractive(): This checks whether the current session is interactive. An interactive session means that a user is actively logged in and interacting with the system, typically through a UI (User Interface). The method .isInteractive() returns true for an interactive session; otherwise, it returns false.

Important :- Don't harcode the sys_id .Instead of hardcoding system IDs directly into your script, consider defining them as constants or retrieving them from property after defining it .

use gs.getProperty('Name')

RaviGaurav_1-1714896667104.png 

RaviGaurav_2-1714896691853.png

 

 

Dynamic Filter

Act as a filter condition to present the data to the user. It is typically involves modifying queries or conditions at runtime based on user input, session data, or other dynamic factors.

It is bascially present on Table ( if the type is reference and when you choose reference table , you can see the option to select dynamic)

RaviGaurav_0-1714896438558.png

 


All of the Above is shown with use case an Demo Below :-

 

Hello ServiceNow Family,

 

Check out the latest video for serviceNow interview preparation with scenarios.

 

Link :- (Day 3-4) ServiceNow Scenario-Based Interview Question | Before Query Business Rule | Dynamic Filter 

Comments
Gillerla Rajesh
Tera Contributor

Using ACL also we will restrict data right, can we achive same thing via ACL?

 

Can we restrict row level data with ACL ?

Mwatkins
ServiceNow Employee
ServiceNow Employee

@Gillerla Rajesh For an in-depth comparison of Business Rule vs. ACL see Query Business Rules vs. ACL - comparison.

 

Also, I would recommend reading through the comments of that article for information (and Performance Best Practices for Before Query Business Rules) about the potential performance issues that Before Query Business Rules can cause.

 

Please Correct if this solves your issue and/or 👍 if Helpful

"Simplicity does not precede complexity, but follows it" - Alan Perlis

Version history
Last update:
‎05-05-2024 01:38 AM
Updated by:
Contributors