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

Enhancing Data Security and Performance with ServiceNow Query Business Rules 🚀

arslan
Kilo Guru

In the fast-paced world of ServiceNow, data management and security are paramount. One often overlooked but powerful tool in the ServiceNow arsenal is the Query Business Rule. These rules allow you to manipulate data at the query level, enhancing performance, enforcing security, and providing more refined data control. Let’s dive into what Query Business Rules are, when to use them, and best practices for implementation.

 

What Are Query Business Rules?

Query Business Rules are server-side scripts that run before a query is executed on a table. Unlike regular business rules that react to inserts, updates, or deletes, Query Business Rules specifically modify or restrict the data being queried. They are particularly useful for

  • Filtering Data: Restrict access to records based on user roles or conditions.
  • Data Security: Hide sensitive information from unauthorized users.
  • Dynamic Conditions: Add conditions dynamically to refine the data fetched.

When to Use Query Business Rules

  1. Enforce Data Security: Restrict access to sensitive records based on user roles, departments, or conditions that are not easily manageable through ACLs.
  2. Improve Performance: Reduce the amount of data returned by adding filters at the query level, enhancing the performance of your instance.
  3. Dynamic Data Customization: Automatically adjust the data returned to users based on real-time conditions, like location, department, or other dynamic criteria.

Example: Securing Sensitive Records

Let’s say you have a table called Incident and you want to restrict access to certain high-priority incidents for users who do not have the itil_admin role. Here’s how you can set up a Query Business Rule:

  1. Navigate to: System Definition > Business Rules.
  2. Create a New Rule: Set the Table to Incident and check the Advanced checkbox.
  3. Script: Add a script to filter the query.
javascript

 

(function executeRule(current, previous /*null when async*/) {
// Check if the user does not have the required role 
if (!gs.hasRole('itil_admin')) { 
// Add a condition to filter out high-priority incidents
 current.addQuery('priority', '!=', '1'); 
gs.info('Query Business Rule applied: High-priority incidents are hidden for non-admin users.'); } })(current, previous);

 

 

Add Or condition in Query Business rule

Example : We have added a confidential checkbox on the table and only sponsor or owner of the ticket should be able to access the ticket once it is marked as confidential and all the other tickets where confidential is false. 

 

we will use the two encoded queries and merge them as explained below:

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

	//Sponser or ownder is current logged in user 
        var query = "u_confidential=true^ownerDYNAMIC"+gs.getUserID()+"^ORsponsorDYNAMIC"+gs.getUserID();
	var orQuery = "^NQu_confidential=false";

	var mergedQuery = query + orQuery +"^"+ current.getEncodedQuery();

	current.addEncodedQuery(mergedQuery);
}

 

Best Practices for Query Business Rules

  1. Minimize Performance Impact: Keep the scripts lightweight and avoid complex queries that could slow down your system.
  2. Test Thoroughly: Test in a non-production environment to ensure that your rule works as expected without unintended side effects.
  3. Use with Caution: Be careful with overly restrictive conditions that might hide too much data or impact essential functions.
  4. Document Your Rules: Always document what each Query Business Rule is doing, why it’s there, and what its intended effect is.

Wrapping Up

Query Business Rules are a powerful way to manage data visibility and enhance the performance of your ServiceNow instance. They provide a layer of dynamic control that can be fine-tuned to meet your organization’s needs. Whether you’re securing data, refining queries, or customizing user experiences, mastering Query Business Rules is a valuable skill for any ServiceNow professional.

Are you using Query Business Rules in your instance? Share your experiences or tips in the comments! Let’s keep the conversation going. 

1 ACCEPTED SOLUTION

Good Addition. Normally Or Condition doesn't work as expected unless we use as merge query.  

 


@onwork1948 wrote:

ServiceNow Query Business Rules are an effective method for enhancing both data security and performance within your ServiceNow environment. These rules enable you to dynamically filter records based on user roles, permissions, or specific conditions, ensuring that only authorized users can access sensitive information. Below are some key ways to leverage them:

  1. Data Security:

    • Query Business Rules are instrumental in enforcing role-based access control (RBAC). By filtering data at the query level, you can ensure that users only retrieve records that match their access permissions.
    • You can apply these rules to restrict data for specific users or groups, preventing unauthorized viewing of sensitive records such as HR or financial data. Additionally, combining Query Business Rules with system properties can create flexible security conditions that adapt to different business requirements.
  2. Optimized Performance:

    • Using Query Business Rules allows you to limit the volume of data being processed and returned by the system, which can significantly improve performance. By narrowing down the dataset before the query is executed, you reduce the load on your system and database, leading to faster response times and improved user experience.
    • It's also beneficial to use indexed fields in your conditions to further enhance performance. Ensure that Query Business Rules do not introduce unnecessary complexity, as overly complex queries may counteract the intended performance improvements.
  3. Best Practices:

    • Efficient Query Design: Ensure that your queries are efficient by limiting the number of conditions and avoiding unnecessary joins or subqueries.
    • Testing: Test your Query Business Rules thoroughly in non-production environments to verify that they don't inadvertently restrict legitimate data access or introduce performance bottlenecks.
    • Documentation: Properly document your Query Business Rules, including the purpose and conditions applied, so that other administrators and developers can easily understand and maintain them. For info visit this page.

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

	//Sponser or ownder is current logged in user 
        var query = "u_confidential=true^ownerDYNAMIC"+gs.getUserID()+"^ORsponsorDYNAMIC"+gs.getUserID();
	var orQuery = "^NQu_confidential=false";

	var mergedQuery = query + orQuery +"^"+ current.getEncodedQuery();

	current.addEncodedQuery(mergedQuery);
}

 

View solution in original post

2 REPLIES 2

onwork1948
Giga Contributor

ServiceNow Query Business Rules are an effective method for enhancing both data security and performance within your ServiceNow environment. These rules enable you to dynamically filter records based on user roles, permissions, or specific conditions, ensuring that only authorized users can access sensitive information. Below are some key ways to leverage them:

  1. Data Security:

    • Query Business Rules are instrumental in enforcing role-based access control (RBAC). By filtering data at the query level, you can ensure that users only retrieve records that match their access permissions.
    • You can apply these rules to restrict data for specific users or groups, preventing unauthorized viewing of sensitive records such as HR or financial data. Additionally, combining Query Business Rules with system properties can create flexible security conditions that adapt to different business requirements.
  2. Optimized Performance:

    • Using Query Business Rules allows you to limit the volume of data being processed and returned by the system, which can significantly improve performance. By narrowing down the dataset before the query is executed, you reduce the load on your system and database, leading to faster response times and improved user experience.
    • It's also beneficial to use indexed fields in your conditions to further enhance performance. Ensure that Query Business Rules do not introduce unnecessary complexity, as overly complex queries may counteract the intended performance improvements.
  3. Best Practices:

    • Efficient Query Design: Ensure that your queries are efficient by limiting the number of conditions and avoiding unnecessary joins or subqueries.
    • Testing: Test your Query Business Rules thoroughly in non-production environments to verify that they don't inadvertently restrict legitimate data access or introduce performance bottlenecks.
    • Documentation: Properly document your Query Business Rules, including the purpose and conditions applied, so that other administrators and developers can easily understand and maintain them. For info visit this page.

Good Addition. Normally Or Condition doesn't work as expected unless we use as merge query.  

 


@onwork1948 wrote:

ServiceNow Query Business Rules are an effective method for enhancing both data security and performance within your ServiceNow environment. These rules enable you to dynamically filter records based on user roles, permissions, or specific conditions, ensuring that only authorized users can access sensitive information. Below are some key ways to leverage them:

  1. Data Security:

    • Query Business Rules are instrumental in enforcing role-based access control (RBAC). By filtering data at the query level, you can ensure that users only retrieve records that match their access permissions.
    • You can apply these rules to restrict data for specific users or groups, preventing unauthorized viewing of sensitive records such as HR or financial data. Additionally, combining Query Business Rules with system properties can create flexible security conditions that adapt to different business requirements.
  2. Optimized Performance:

    • Using Query Business Rules allows you to limit the volume of data being processed and returned by the system, which can significantly improve performance. By narrowing down the dataset before the query is executed, you reduce the load on your system and database, leading to faster response times and improved user experience.
    • It's also beneficial to use indexed fields in your conditions to further enhance performance. Ensure that Query Business Rules do not introduce unnecessary complexity, as overly complex queries may counteract the intended performance improvements.
  3. Best Practices:

    • Efficient Query Design: Ensure that your queries are efficient by limiting the number of conditions and avoiding unnecessary joins or subqueries.
    • Testing: Test your Query Business Rules thoroughly in non-production environments to verify that they don't inadvertently restrict legitimate data access or introduce performance bottlenecks.
    • Documentation: Properly document your Query Business Rules, including the purpose and conditions applied, so that other administrators and developers can easily understand and maintain them. For info visit this page.

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

	//Sponser or ownder is current logged in user 
        var query = "u_confidential=true^ownerDYNAMIC"+gs.getUserID()+"^ORsponsorDYNAMIC"+gs.getUserID();
	var orQuery = "^NQu_confidential=false";

	var mergedQuery = query + orQuery +"^"+ current.getEncodedQuery();

	current.addEncodedQuery(mergedQuery);
}