Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Use of Display and Query business rule

snowlearner
Kilo Expert

Can anyone tell me the use of display and query business rule.

Also whats the difference between the two?

Thanks.

7 REPLIES 7

Pradeep Sharma
ServiceNow Employee
ServiceNow Employee

The SN Nerd
Giga Sage
Giga Sage

Two common use cases


Display - for when you want to run server side code onLoad


When an incident is opened, display the number of related open incidents.



Query - for removing the annoying 'results hidden from ACL'


An ACL is restricting 10 record in a list. The list says '10 records hidden by security' - I want it to show 0 record.


Create a query business rule that mimics the read ACL condition for that table.



ServiceNow Nerd
ServiceNow Developer MVP 2020-2022
ServiceNow Community MVP 2019-2022

1__vipind
Tera Contributor

A Query Business Rule is one of the available operation types in Before, After, and Async Business Rules. It executes when the form is loaded and is used to filter the records that a user is allowed to see when querying a table. It works at both the form level and the list level.

For example, if you want the current logged-in user to see only those records where they are the caller or the assigned_to, you can write:

Use Case

If you want the logged-in user to see only those records where they are the caller or the assigned_to, you can write:

Steps

  1. Go to Business Rules

  2. Create a new rule → Before Business Rule

  3. Open the Advanced view

  4. Set When to run = Query

  5. In the Script field: current.addQuery('caller', gs.getUserID()); // Filter records where logged-in user is caller
    // OR
    current.addQuery('assigned_to', gs.getUserID()); // Filter records where logged-in user is assigned_to

    Result

    When the user logs in and opens the list view, they will see only those records in which:

    • They are the caller, or

    • They are in assigned_to.

 

Display Business Rule

A Display Business Rule runs when a form is loaded.
It does not support operations like insert, update, delete, or query.

It is mainly used to:

  • Fetch information from the server

  • Send it to the client script
    For this, we use g_scratchpad, which is an empty object available on both server and client.

Example – Show Logged-in User’s Name

  1. Create a Display Business Rule

  2. In the Script field:

 

 
 
 

g_scratchpad.name = "Vipin Dimri"; // Passing a hardcoded value to the client

 

In the Client Script (onLoad):

alert(g_scratchpad.name);

Display BR Without g_scratchpad

Sometimes Display BR is used only to pre-populate fields and doesn't need g_scratchpad.
For example, populating Problem Task fields from the related Problem record.

Use Case – Populate Problem Task with Problem Data

 

current.problem != "" && current.isNewRecord()

 

current.assignment_group = current.problem.assignment_group;
current.short_description = current.problem.short_description;