What is the Difference between Display BR and onLoad client script other than one is server side script and another is client side
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-08-2021 11:30 AM
Difference between Display BR and onLoad client script with demo in personal instance
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
2 weeks ago
Hi @Danish8 ,
Both Display Business Rules and onLoad Client Scripts execute during form loading, but they serve different purposes.
The main difference is how they handle data and interact with the form.
1. Display Business Rule
-
Executes on the server when a record form is requested.
-
Runs before the form is presented to the user.
-
Can access server-side objects such as
currentandgs. -
Its primary purpose is to pass server-side information to the client using
g_scratchpad. -
It does not directly control the browser UI.
2. onLoad Client Script
-
Executes in the user's browser when the form loads.
-
Can interact with form fields using
g_form. -
Can show messages, hide fields, make fields mandatory or read-only, etc.
-
Can access information passed from a Display Business Rule through
g_scratchpad.
3. Practical Example
Scenario: When opening an Incident, display a message if the caller belongs to a particular department. Assume the department information is not available on the form.
Step 1: Create a Display Business Rule
Navigate to System Definition → Business Rules.
Configure:
-
Name: Get Caller Department
-
Table: Incident
-
When: Display
-
Active: True
Script:
(function executeRule(current, previous) {
if (!current.caller_id.nil()) {
g_scratchpad.callerDepartment =
current.caller_id.department.getDisplayValue();
}
})(current, previous);
This retrieves the caller's department on the server and passes it to the browser through g_scratchpad.
Step 2: Create an onLoad Client Script
Navigate to System Definition → Client Scripts.
Configure:
-
Name: Display Caller Department
-
Table: Incident
-
Type: onLoad
-
UI Type: All
Script:
function onLoad() {
if (g_scratchpad.callerDepartment == 'IT') {
g_form.addInfoMessage(
'The caller belongs to the IT department.'
);
}
}
When should you use them together?
Suppose you need information from the database that is not available on the form and want to use it to control the form UI. Use a Display Business Rule to retrieve that information and g_scratchpad to pass it to an onLoad Client Script.
If the required information is already available on the form, an onLoad Client Script alone may be sufficient. You don't need a Display Business Rule unnecessarily.
Official documentation:
Hope this practical example clarifies the difference! please mark the response as helpful and accept it as the solution.
Regards,
Sandesh Powar
ServiceNow Rising Star