The Now Platform® Washington DC release is live. Watch now!

Help
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Mayu
Giga Guru
Giga Guru

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 

  • When to run 
  • Action

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 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

  • 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

Comments
Mwatkins
ServiceNow Employee
ServiceNow Employee

WARNING!

Every year we see multiple customer escalations for performance because of poorly written before/query business rules. 

The example given in this article, for example, is not a scalable solution. It is bad practice to use multiple OR conditions or CONTAINS queries in commonly executed scripts.

If you decide to use before/query business rules as an alternative to ACL's please read the following article carefully!

Performance Best Practice for Before Query Business Rules

Please Correct if this solves your issue and/or 👍 if Helpful

"Simplicity does not precede complexity, but follows it"

Neha Patil
Tera Explorer

What is the main purpose of query  business rule?

Himanshu42
Tera Contributor

@Neha Patil 

The primary purpose of a business rule is to perform a predefined set of actions that need to be performed when a record is displayed, inserted, updated, or deleted or when a table is queried. It can be achieved by any of below four types of BR's:
1) before business rule.
2) after business rule.
3) async business rule.
4) display business rule.

JenniferLois
Tera Contributor

Thank goodness! I was looking for a business rule for myself to kick-start an online business. The business is going to assist students who want UK Assignment Help. I hope my business goes well and it could help a lot of students. This was really helpful I love it.

Version history
Last update:
‎05-18-2021 08:15 PM
Updated by: