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