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

Display Business Rules in details -https://servicenowhelpdesk.com

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 Display Business Rule In ServiceNow

What is 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.

Display rule basically runs in two cases, which are as follows:-

        1. Display business rule provide server side data to client side
        2. Display business rule is triggered whenever a user open / loads form.

Example of display business rule:

We need to update the number of views on a comment or post. So, for that Display BR is being used.

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

Scenario 1 – We need to set certain values on incident form if the logged in user is member of a particular group.

To achieve this use case, we first need to create a Display BR and then call the same in our client script. Please refer to the below attached screenshots.

 

Here is the BR to fulfill our work at server side.

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


    g_scratchpad.fac = (gs.getUser().isMemberOf("a56f4cbfdb252410b9450342f396191c"));
    
})(current, previous);

 

Now, moving onto the client-side scripting to see how this g_scratchpad is called, and our requirement is achieved.

function onLoad() {
    //Type appropriate comment here, and begin script below

    var a = g_form.getValue('number');

    
    if (g_scratchpad.fac == true) {

        var pro = g_scratchpad.property;
        var myArr = pro.split(",");
        var pointer = "0";
        for (var i = 0; i < myArr.length; i++) {
            g_form.removeOption('u_business_service', myArr[i]);
        }
        if (content != '') {
            //alert('script 2');
            g_form.setValue('u_business_service', '10’);
            g_form.setValue('category', '1');
            g_form.setValue('subcategory', '2');
        }
    }

 

Scenario 2- We need to populate the caller email id if incident has any attachment.

First, we will have the BR with the following script.

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

                            g_scratchpad.hasAttachments = current.hasAttachments();

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

})(current, previous);

 

Now, we will move towards our client script part.

function onLoad() {  

   var email=g_scratchpad.email;

if(g_scratchpad.hasAttachments){

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

}

}

 

FAQ

LEAVE A REPLY

Please enter your comment!
Please enter your name here