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

 

What is Async Business Rule?

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.

Async business rules are like after rules in that they run after the database commits a change. Unlike after rules, async rules run in the background simultaneously with other processes. Async business rules allow ServiceNow to return control to the user sooner but may take longer to update related objects. 

 

Example of async business rule:

Now when someone comments on your post or like it, there is also some notification being send to the concerned person. It does not need to be immediate. We can have an async rule to trigger the email.

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

Scenario 1 – Send a notification to user whenever Category is set as Security Breach.

We have several steps to follow to complete this scenario. First of all, we will create an event registry to be used in our Business Rule.

Now the second step will be creating an Async Business Rule. Please refer the image below for the same.

Here is the script written to call the Event through Business rule via event queue function.

Lastly, we will use this event registry in our notification.

You can now write what it will contain according to your requirement and notification will be triggered each time category changes to security breaches.

Scenario 2 – Attachment add to a particular variable in a certain catalog item should get attach to the record as well.

We all are aware that attachment made to a variable in any catalog item is not attached to the record itself and make it somehow difficult to send the same attachment via notification for the respective record. So, to achieve the same we are using Async Business Rule. Now, why async not after BR the answer is attachment might take time to get attach to the record so to make this smooth and without blocking user experience, we will be using this.

We will start by creating a new Async BR filling all the required fields, here we want this BR for a particular catalog item, so we are mentioning the same under filter conditions.

 

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

function executeRule(current, previous /*null when async*/ ) {
   
    var gr = new GlideRecord("sys_attachment");
    gr.addQuery("table_name", "ZZ_YY" + current.getTableName());
    gr.addQuery("table_sys_id", current.sys_id);
    gr.query();
    if (gr.next()) {
        gr.table_name = current.getTableName();
        gr.update();
        new global.VariableUtil().copy(gr.sys_id, current.getTableName(), current.sys_id); 
    }


})(current, previous);

 

FAQ

 

LEAVE A REPLY

Please enter your comment!
Please enter your name here