Help needed for Query BR in ServiceNow

SandeepKSingh
Kilo Sage

What is query BR? What is primary objective of it? Explain it with real time use case? Please help m

3 ACCEPTED SOLUTIONS

Ravi Gaurav
Giga Sage
Giga Sage

Hey @SandeepKSingh 

You can follow the below example :-

 

RaviGaurav_0-1724423354800.png

To Restrict Table Data :-

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

    if(gs.getUser().isMemberOf('6df8cc46474d02901279750cd36d43e4') && gs.getSession().isInteractive()){
        current.addQuery('assignment_group','6df8cc46474d02901279750cd36d43e4'); // give your assignment group sys_id. Its just an example you can define sys_id in property and use it for best practice.
    }
    else{
        return;
    }

})(current, previous);
--------------------------------------------------------------------------------------------------------------------------


If you found my response helpful, I would greatly appreciate it if you could mark it as "Accepted Solution" and "Helpful."
Your support not only benefits the community but also encourages me to continue assisting. Thank you so much!

Thanks and Regards
Ravi Gaurav | ServiceNow MVP 2025,2024 | ServiceNow Practice Lead | Solution Architect
CGI
M.Tech in Data Science & AI

ï”— YouTube: https://www.youtube.com/@learnservicenowwithravi
ï”— LinkedIn: https://www.linkedin.com/in/ravi-gaurav-a67542aa/

View solution in original post

Najmuddin Mohd
Mega Sage

Hi @SandeepKSingh 

One use case is that you can hide the records from displaying to user.

If the user is having a particular role, then only display all records
Else, display only limited records.

Example:

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

    // Check if the user is in the "Network" assignment group
    var userInGroup = gs.getUser().isMemberOf('Network');

    // If the user is NOT in the "Network" group, restrict access to priority "1 - Critical" incidents
    if (!userInGroup) {
        current.addQuery('priority', '!=', '1');
    }

})(current, previous);


From the above script, if the user is not member of Network team then he can not see any Incident with P1.

 

If this information helps you, Kindly mark it as Helpful.

Regards,
Najmuddin.

View solution in original post

Ravi Gaurav
Giga Sage
Giga Sage

 

Hi @SandeepKSingh 

one more example :-

Query Business Rule (BR) in ServiceNow is a type of business rule that is executed when a database query is run, before any records are retrieved. Its primary objective is to manipulate the records being queried, such as filtering, adding conditions, or modifying field values before the data is fetched.

Primary Objective of Query Business Rule

  • Data Security: Ensure that only relevant data is retrieved, especially when access restrictions are needed.
  • Data Integrity: Modify or validate data before it's displayed to the user.
  • Performance Optimization: Reduce the amount of data being retrieved to improve performance.

Real-Time Use Case Example

Imagine you are working on a ServiceNow instance for a company that has multiple departments: HR, IT, and Finance. The HR team has a requirement where they should only see incidents related to employee issues, while the IT team should only see incidents related to technical issues.

Scenario: When an HR employee accesses the incident table, you want to ensure they only see incidents categorized as "Employee Issues."

How to Use a Query Business Rule in This Scenario

  1. Create the Query Business Rule:

    • Table: Incident (incident)
    • When to Run: Before the query is executed
    • Condition: Check if the current user belongs to the HR department.
    • Script:
      if (gs.hasRole('hr_role')) { current.addQuery('category', 'Employee Issues'); }
  2. Explanation:

    • Role Check: The gs.hasRole('hr_role') checks if the logged-in user has the HR role.
    • Adding Query: The current.addQuery('category', 'Employee Issues'); ensures that only incidents with the category "Employee Issues" are retrieved for HR users.
  3. Result:

    • When an HR employee logs in and accesses the incident list, they will only see incidents related to employee issues. This ensures that HR users are not overwhelmed with irrelevant incidents and that sensitive technical issues are not exposed to non-IT staff.

Example Code:

Here’s how the Query BR might look:

 

(function executeRule(current, previous /*null when async*/) { // Check if the user is in HR if (gs.hasRole('hr_role')) { // Add condition to only show 'Employee Issues' incidents current.addQuery('category', 'Employee Issues'); } })(current, previous);

 

--------------------------------------------------------------------------------------------------------------------------


If you found my response helpful, I would greatly appreciate it if you could mark it as "Accepted Solution" and "Helpful."
Your support not only benefits the community but also encourages me to continue assisting. Thank you so much!

Thanks and Regards
Ravi Gaurav | ServiceNow MVP 2025,2024 | ServiceNow Practice Lead | Solution Architect
CGI
M.Tech in Data Science & AI

ï”— YouTube: https://www.youtube.com/@learnservicenowwithravi
ï”— LinkedIn: https://www.linkedin.com/in/ravi-gaurav-a67542aa/

View solution in original post

3 REPLIES 3

Ravi Gaurav
Giga Sage
Giga Sage

Hey @SandeepKSingh 

You can follow the below example :-

 

RaviGaurav_0-1724423354800.png

To Restrict Table Data :-

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

    if(gs.getUser().isMemberOf('6df8cc46474d02901279750cd36d43e4') && gs.getSession().isInteractive()){
        current.addQuery('assignment_group','6df8cc46474d02901279750cd36d43e4'); // give your assignment group sys_id. Its just an example you can define sys_id in property and use it for best practice.
    }
    else{
        return;
    }

})(current, previous);
--------------------------------------------------------------------------------------------------------------------------


If you found my response helpful, I would greatly appreciate it if you could mark it as "Accepted Solution" and "Helpful."
Your support not only benefits the community but also encourages me to continue assisting. Thank you so much!

Thanks and Regards
Ravi Gaurav | ServiceNow MVP 2025,2024 | ServiceNow Practice Lead | Solution Architect
CGI
M.Tech in Data Science & AI

ï”— YouTube: https://www.youtube.com/@learnservicenowwithravi
ï”— LinkedIn: https://www.linkedin.com/in/ravi-gaurav-a67542aa/

Najmuddin Mohd
Mega Sage

Hi @SandeepKSingh 

One use case is that you can hide the records from displaying to user.

If the user is having a particular role, then only display all records
Else, display only limited records.

Example:

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

    // Check if the user is in the "Network" assignment group
    var userInGroup = gs.getUser().isMemberOf('Network');

    // If the user is NOT in the "Network" group, restrict access to priority "1 - Critical" incidents
    if (!userInGroup) {
        current.addQuery('priority', '!=', '1');
    }

})(current, previous);


From the above script, if the user is not member of Network team then he can not see any Incident with P1.

 

If this information helps you, Kindly mark it as Helpful.

Regards,
Najmuddin.

Ravi Gaurav
Giga Sage
Giga Sage

 

Hi @SandeepKSingh 

one more example :-

Query Business Rule (BR) in ServiceNow is a type of business rule that is executed when a database query is run, before any records are retrieved. Its primary objective is to manipulate the records being queried, such as filtering, adding conditions, or modifying field values before the data is fetched.

Primary Objective of Query Business Rule

  • Data Security: Ensure that only relevant data is retrieved, especially when access restrictions are needed.
  • Data Integrity: Modify or validate data before it's displayed to the user.
  • Performance Optimization: Reduce the amount of data being retrieved to improve performance.

Real-Time Use Case Example

Imagine you are working on a ServiceNow instance for a company that has multiple departments: HR, IT, and Finance. The HR team has a requirement where they should only see incidents related to employee issues, while the IT team should only see incidents related to technical issues.

Scenario: When an HR employee accesses the incident table, you want to ensure they only see incidents categorized as "Employee Issues."

How to Use a Query Business Rule in This Scenario

  1. Create the Query Business Rule:

    • Table: Incident (incident)
    • When to Run: Before the query is executed
    • Condition: Check if the current user belongs to the HR department.
    • Script:
      if (gs.hasRole('hr_role')) { current.addQuery('category', 'Employee Issues'); }
  2. Explanation:

    • Role Check: The gs.hasRole('hr_role') checks if the logged-in user has the HR role.
    • Adding Query: The current.addQuery('category', 'Employee Issues'); ensures that only incidents with the category "Employee Issues" are retrieved for HR users.
  3. Result:

    • When an HR employee logs in and accesses the incident list, they will only see incidents related to employee issues. This ensures that HR users are not overwhelmed with irrelevant incidents and that sensitive technical issues are not exposed to non-IT staff.

Example Code:

Here’s how the Query BR might look:

 

(function executeRule(current, previous /*null when async*/) { // Check if the user is in HR if (gs.hasRole('hr_role')) { // Add condition to only show 'Employee Issues' incidents current.addQuery('category', 'Employee Issues'); } })(current, previous);

 

--------------------------------------------------------------------------------------------------------------------------


If you found my response helpful, I would greatly appreciate it if you could mark it as "Accepted Solution" and "Helpful."
Your support not only benefits the community but also encourages me to continue assisting. Thank you so much!

Thanks and Regards
Ravi Gaurav | ServiceNow MVP 2025,2024 | ServiceNow Practice Lead | Solution Architect
CGI
M.Tech in Data Science & AI

ï”— YouTube: https://www.youtube.com/@learnservicenowwithravi
ï”— LinkedIn: https://www.linkedin.com/in/ravi-gaurav-a67542aa/