logo

NJP

Business Rule

Import · May 19, 2021 · article

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 .

Four type of business rule.

  1. before business rule.
  2. after business rule.
  3. async business rule.
  4. display business rule.

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.

Two steps of business rule

Before Business Rule :

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

Step Of Execution :

Record get Save into Database or Table
Before Business Rule will get Execute
When user click save or update record
  • Example : Check the current value of state, if not equal to previous value then set impact high .

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

  • Name: incident query
  • Table: Incident
  • When: before, insert,update
  • Script:

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

if('current.state'!='previous.state')

{

current.impact=’1’;

current.update();

}

})(current, previous);

After Business Rule :-

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

Step Of Execution :

When user click save or update the record
Record get save into database or table
After Business Rule Script Will get Execute
  • Example :- When Problem is created related Incident state change to “On hold” and onhold reason “Awaiting Caller”

select table problem and apply after business rule perform action insert and update

  • Name: incident query
  • Table: Incident
  • When: After,insert ,update
  • Script:

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

var gr = new GlideRecord('incident');

gr.addActiveQuery(); //Cross check only active incidents

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
Record get save into database or table
Aync Business Rule Script will get execute
  • Example :- When assignment group changes then update the task SLA group to current assignment group .

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

var value = current.u_company // Add your code here var gr = new GlideRecord('u_gliderecord'); gr.initialize(); gr.u_company= value;

gr.insert();

})(current, previous);

Display Business Rule :-

1.display business rule provide client script access from server side data.

  1. Display business rule is triggered whenever a user open / loads form.

Example :-Populate the caller email id if incident has any attachment.

Display Business Rule

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

g_scratchpad.hasAttachments = current.hasAttachments();

g_scratchpad.email=current.caller_id.email.getDisplayValue();

})(current, previous);

Client Script

function onLoad() {

var email=g_scratchpad.email;

if(g_scratchpad.hasAttachments){

alert("Caller Email name==="+email);

}

}

Two Parameter :

1)current : current referenced current value of the Form .If we want to access current value using current parameter.

2)Previous :Previous referenced previous value of the table. If we want to access any old value using previous parameter.

Query Business Rule :Query business rule alternative way of ACl in query business it gives limited access to particular use*r.*

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

  • Name: incident query
  • Table: Incident
  • When: before, query
  • Script:

.if(!gs.hasRole("itil") && gs.isInteractive()) {

var u = gs.getUserID();

var qc = current.addQuery("caller_id",u).addOrCondition("opened_by",u).addOrCondition("watch_list","CONTAINS",u);

gs.print("query restricted to user: " + u);

}

Please Mark it Helpful & Book Mark It

Thanks ,

Mayuri Mandavkar

Labels:

image

View original source

https://www.servicenow.com/community/developer-articles/business-rule/ta-p/2329993