Before Business Rule In ServiceNow

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. We use business rules to achieve tasks such as create events for email notifications and script actions.

We have 4 type of business rule but in this article we will only focus on before business rule.

    1. Before business rule
    2. After business rule
    3. Async business rule
    4. Display business rule

Lets begin to understand Before Business Rule In ServiceNow

Before Business Rule as name itself justify works Before a record is saved to the database. Business rules execute after form submission and before the record update in the database. It runs before the database operation, so no extra operations are required.

Before Business Rule - https://servicenowhelpdesk.com

As we can see in the above image that once the form is being submitted in ServiceNow, the BR rule is executed and after that any change is done in database.

Example of before business rule:

When someone is posting any comment or material on your post, and you need to confirm that it does not contain anything offensive. In this case you will be using the Before Business Rule.

To have a better understanding of this concept let’s have a look at below mentioned scenarios.

 

Scenario 1 – Set the priority for incident assigned to a particular group.

To achieve this, we will be writing a before business rule and define the assignment group in filter condition. Please refer below mentioned image.

Before Business Rule - https://servicenowhelpdesk.com

Now we will move to the script part.

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

    current.impact='Low';
    current.urgency='Low';

})(current, previous);

We all are aware that impact and urgency needs to be defined for populating priority. Here we are using the Before rule so that rule is executed first and changes are made to the database according to the script written.

 

Scenario 2 – We need to copy the short description from variable present on RITM to the short description of catalog task.

We will start by creating a new Before BR filling all the required fields.

Here is the script to achieve the above-mentioned scenario.

(function executeRule(current, previous /*null when async*/ ) {
    
    current.short_description = current.request_item.variables.short_description;
    //current.update();
    // Add your code here


})(current, previous);

 

Scenario 3 – To delete the duplicate attachment from service portal’s catalog item

We will start by simply writing a Before BR.

Here is the script to achieve the above-mentioned scenario.

(function executeRule(current, previous /*null when async*/) {
    
    var a =new GlideRecord('sys_attachment');
    a.addQuery('table_name','LIKE','sp_portal');
    a.addQuery('file_name','=',current.file_name);
    a.orderBy('table_sys_id');
    a.orderByDesc('sys_created_on');
    a.query();
    
    while(a.next()){
    
        
        if(a.table_sys_id==current.table_sys_id){
            a.deleteRecord();
        }
    }
    
})(current, previous);

 

FAQ

 

LEAVE A REPLY

Please enter your comment!
Please enter your name here