Business Rule Complete Notes
Business Rule Complete Notes
• The Glide Record API is a primary means of interfacing with the database on the server-side code.
• A Glide Record is a Object that contain Record from single table [EX= incident , problem , change
etc].
• Use this API to represent a Glide Record & add Query parameter, filter, Limit, Ordering etc…
• var inc = new GlideRecord ('incident’) //GlideRecord is main Oject and Incident is Table
•}
• inc.query ();
• while(inc.next()){
• gs.print(inc.number);
•}
• inc.query ();
• while(inc.next()){
• gs.print (inc.number);
•}
we can use addEncodedQuery method Instead of passing multiple queries into our script
• Step-6: Script
• inc.query();
• while(inc.next()){
• gs.print(inc.number);
•}
•=
• !=
•>
• >=
•<
• <=
•=
• !=
• IN
• NOT IN
• STARTSWITH
• ENDSWITH
• CONTAINS
• inc.addActiveQuery();
• inc.addQuery('priority','<=',2);
• inc.query();
• while(inc.next()){
• gs.print(inc.number);
•}
• inc.addActiveQuery();
• inc.addQuery('priority','<=',2);
• inc.addQuery('short_description','CONTAINS’,’test’);
• inc.query();
• while(inc.next()){
•}
Result:-Print all records where our condition meet like (<=2 and CONTAINS)
• inc.query();
• while(inc.next()) {
•}
• inc.addQuery('category=software');
• inc.orderBy('short_description');
• inc.query();
• while(inc.next()){
•}
• inc.addQuery('priority=1');
• inc.orderByDesc('short_description');
• inc.setLimit(10);
• inc.query();
• while(inc.next()){
•}
These methods are used to Inserts a new record using the field values that have been
• inc.priorty = 1;
Business Rule:- 1. A business rule is a server-side script that runs when a record is displayed,
inserted, updated, or deleted, or when a table is queried.
2.Table of business rule:- sys_script .
Database Operation :
1. Insert:- When the user creates a new record and the system inserts it into the database.
2. Update :- When the user modifies an existing record.
3. Query :- Before a query for a record or list of records is sent to the database. Typically
you should use query for before business rules.
4. Delete :- When the user deletes a record.
• When to run
• Action
Global Variable
• Current: Current state of the record being referenced. See "Prevent null pointer
exceptions" below to check for nulls before using this variable.
• Previous: If multiple updates are made to the record within one execution context,
previous will continue to hold the state of the record before the first update or delete
operation. Available on update and delete operations only. Not available on async
operations. See "Prevent null pointer exceptions" below to check for nulls before using
this variable.
• G_scratchpad: Scratchpad object is available on display rules, and is used to pass
information to the client to be accessed from client scripts.
• Gs: glide system
Business rule processing flow
Avoid using current.update() in a business rule script. The update() method triggers business rules
to run on the same table for insert and update operations, leading to a business rule calling itself
over and over. Changes made in before business rules are automatically saved when all before
business rules are complete, and after business rules are best used for updating related, not
current, objects. When a recursive business rule is detected, the system stops it and logs the error
in the system log. However, current.update() causes system performance issues and is never
necessary.
You can prevent recursive business rules by using the setWorkflow() method with the false
parameter. The combination of the update() and setWorkflow() methods is only recommended in
special circumstances where the normal before and after guidelines mentioned above do not
meet your requirements.
current.impact=’1’;
current.update();
})(current, previous);
gr.addQuery('problem_id', current.sys_id);
gr.query();
while (gr.next()) {
gr.state='3';
gr.hold_reason='1';
gr.update();
})(current, previous);
Async Business Rule :-
Async Business Rule Execute after the action perform on database or table.Whent data saved
into database then Async business rule Run. Async Business Rule task are independent to
each other.They are run simultaneouly reduce the redandency.
Step Of Execution :
When user click save and update the record
Example :- When assignment group changes then update the task SLA group to current
assignment group .
gr.initialize();
gr.u_company= value;
gr.insert();
})(current, previous);
Query Business Rule :Query business rule alternative way of ACl in query business it gives
limited access to particular user.
Example :prevents users from accessing incident records unless they have the itil role, or are
listed in the Caller or Opened by field. So, for example, when self-service users open a list of
incidents, they can only see the incidents they submitted
Example
After Br
prb.initialize();
prb.first_reported_by_task=current.sys_id;
prb.short_description=current.short_description;
prb.description=current.short_description;
prb.assignment_group=current.assignment_group;
prb.insert();
Create a problem record on incident form related tab
After Br
prb.initialize();
prb.short_description=current.short_description;
prb.description=current.short_description;
prb.insert();
current.problem_id=prb.sys_id;
current.update();
When problem state changes to resolved all incident related to it get closed
After Br
Inc.addQuery(‘problem_id’,current.sys_id);
Inc.query();
While(Inc.next()){
Inc.state=6;
Inc.update();
After BR : This type of BR is used when some field changes needs to be reflected immediately
after user saves the record. Mostly used to update/insert record into another table once
insert/update/delete operation is performed on current record.
- Current
- Previous
- gs
- g_scratchpad
Note: Previous object is null in Asynch BR, as there could be multiple updates on same record
while BR is in queue for execution which might create conflict on which version needs to be
considered as previous.
What is display BR? When to use it? Explain with real time use case?
Display rules are processed when a user requests a record form. The data is read from the
database, display rules are executed, and the form is presented to the user.
The primary objective of display rules is to use a shared scratchpad object, g_scratchpad, which is
also sent to the client side as part of the form. This can be useful when you need to build client
scripts that require server data which is not typically part of the record being displayed.
Real time use case : If logged in user is member of current assignment group then only show
description field.
We can write display BR as shown below to check if logged in user is part of current group or not
and store result in g_scratchpad object, which later can be accessed in client script to show/hide
description field accordingly.
2. Query BRs will affect a GlideRecord query result where ACLs will not unless we are using
GlideRecordSecure class.
3. Query BR shows only those records where filter criteria matched while ACL shows all pages
with the restricted records being invisible and a message at the bottom 'some records removed
due to security'.
A global Business Rule is a Business Rule where the selected Table is Global. Any other script can
call global Business Rules. Global Business Rules have no condition or table restrictions and load
on every page in the system.
We can use 'current.setAbortAction(true)' in Business Rule script to abort action. This will stop
data updation in database.
We can use current.operation() in BR. Depending on current operation, it returns update, delete
or insert value.
Syntax : gs.eventQueue('event_name',current,'param1','param2');
GlideRecord queries and manipulates data in the same way as GlideRecordSecure, but
GlideRecordSecure provides additional features for securing sensitive data. This ensures that
sensitive data cannot be intercepted by unauthorized users as it enforces access controls.
Another difference between the two is performance, Because GlideRecordSecure enforces access
controls, it may take longer to execute queries than GlideRecord.