After Business Rule in ServiceNow

1975

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

What is After Business Rule?

After a record is saved to the database. Business Rules executes after form submission and after the record update in the database. This rule is basically used to update information on related objects that need to be displayed immediately, such as GlideRecord queries.

After business rule in details-https://servicenowhelpdesk.com

It is clear from the above given image how exactly this rule work in our system. When user click save or update the record. Record gets save into database or table. After Business Rule Script Will get Executed.

Example of after business rule:

Let’s say you have passed the Before BR rule and posted your comment successfully. Now someone like your comment or mark it as help we can have an After BR set up to accomplish this.

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

 

Scenario 1 – We need to close the task if the respective RITM moves to Closed incomplete.

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

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

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

    // Add your code here
    var gr = new GlideRecord("sc_task");
    gr.addQuery("request_item", current.request_item);
    gr.addEncodedQuery("stateIN9,-5,1,2,4,7");
    gr.query();
    if (gr.next()) {} else {
        var gr1 = new GlideRecord("sc_req_item");
        //gs.log('this is causing issue line 10');
        gr1.get(current.request_item);
        gr1.state = "10";
        //gs.log('this is causing issue line 13');

        //gr1.assigned_to = current.assigned_to;
        gr1.update();
    }

})(current, previous);

 

Scenario 2 ­- When Problem is created related Incident state should change to “On hold” and on-hold reason “Awaiting Caller”.

We need to start by selecting table as problem and apply after business rule with action as insert and update.

Here is the script to achieve the above-mentioned scenario

(function executeRule(current, previous /*null when async*/ ) {
    var gr = new GlideRecord('incident');
    gr.addQuery('problem_id', current.sys_id);
    gr.query();
    while (gr.next()) {
        gr.state = '3';
        gr.hold_reason = '1';
        gr.update();
    }
})(current, previous);

 

Scenario 3 – We need to update the assignment group for Request with assignment group of respective catalog task.

We will start by simply writing a After BR.

Here is the script to achieve the above-mentioned scenario

(function executeRule(current, previous /*null when async*/ ) {
    // Add your code here
    var gr = new GlideRecord('sc_task');
    gr.addQuery('number', current.number);
    gr.query();
    if (gr.next()) {
        var ritm = new GlideRecord('sc_req_item');
        ritm.addQuery('sys_id', gr.request_item);
        ritm.query();
        if (ritm.next()) {
            ritm.assignment_group = gr.assignment_group;
            var req = new GlideRecord('sc_request');
            req.addQuery('sys_id', ritm.request);
            req.query();
            if (req.next()) {
                req.assignment_group = gr.assignment_group;
                req.update()
           }
        }
    }

 

FAQ

What is async business rule in servicenow?

Async Business Rules run after records are inserted/modified/queried. Async and after works similar the only difference is async run asynchronously as Scheduled Jobs. Async BR are queued by the scheduler to run as soon as they can be fit in. This allows the current transaction to finish without waiting for the rule and give the control back to user to continue working.

What is before business rule in servicenow?

Before BR 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.

What is after business rule in servicenow?

After a record is saved to the database. Business Rules execute after form submission and after the record update in the database.

What is display business rule in servicenow?

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.

What is before query business rule in servicenow?

Other than the four above mentioned rules we have Before Query rule. It is a type of business rule in ServiceNow that we can use to limit that what all records users can access from a given table. In other words, we can also say that it is used for data segregation on an instance. To understand this more clearly see the scenario mentioned below.

 

 

LEAVE A REPLY

Please enter your comment!
Please enter your name here