0

Business rules is the server side script which means that it will execute on server or database. Business rule runs faster than other script in ServiceNow.  The script or code written in business rule area will get executed when record is inserted, displayed, updated, deleted or when table is queried.

The four types of business rule in ServiceNow are:

Display Business Rule
Before Business Rule
After Business Rule
Async Business Rule

Database Operation :

Insert:- When the user creates a new record and the system inserts it into the database.
Update :- When the user modifies an existing record.
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.
Delete :- When the user deletes a record.

Before Business Rule :

Before Business Rule execute before data save into the database or table.

Exmaple

When to run select before  and action is insert or update.

  • Name: Priority Update
  • Table: Incident
  • When: before, insert,update
  • Script:
(function executeRule(current, previous /*null when async*/)
                 if('current.state'!='wip')
                                {
                                                 current.priority='1';
                                            current.update();
                                 }
                        })(current, previous);

After Business Rule :-

After Business Rule Execute after the action perform on database. When data saved into database then after business rule Run.

  • Name: Priority Update
  • Table: Incident
  • When: After,insert ,update
  • Script:
(function executeRule(current, previous /*null when async*/) {
             var updateProblem = new GlideRecord('problem');
         updateProblem.addQuery('parent', current.sys_id);
         updateProblem.query();
         if(gr.next()) {
                            gr.state='3';
                          gr.update();
                     }
 })(current, previous);

Async Business Rule :-

Async business rules are like after business rule but it runs in the background simultaneously with other processes. Means async business rule run after the data is saved into the database.

Display Business Rule :-

Display Business Rules run after the data is read from the database and before the form is presented back to the user. Display Business Rules execute when a user requests a record form.

Business Rule:

(function executeRule(current, previous /*null when async*/ ) {
    var gr = new GlideRecord("problem");
    if (gr.get(current.parent)) {
 g_scratchpad.parent=current.parent;
        g_scratchpad.short_description=gr.short_description;
 g_scratchpad.assigned_to=gr.assigned_to;
 g_scratchpad.assignment_group=gr.assignment_group;
    }
 })(current, previous);

Client Script

function onLoad() {
   //Type appropriate comment here, and begin script below
 if(g_scratchpad.parent) {
  g_form.setValue("short_description",g_scratchpad.short_description);
  g_form.setValue("assignment_group",g_scratchpad.assignment_group);
  g_form.setValue("assigned_to",g_scratchpad.assigned_to);
 }   
}
Business Rules in ServiceNow
Working Code Edited question September 27, 2023