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.
-
- Before business rule
- After business rule
- Async business rule
- 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
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.