What are Business Rules?
In ServiceNow, a Business Rule is JavaScript code that runs at different stages of database operations. It can run before or after an operation like insert, update, or delete. It can also run asynchronously during saving or when loading a list view. These rules automate logic and manage data conditions.
Business Rules are a fundamental part of ServiceNow’s automation capabilities, allowing developers to control what happens when records in the database are manipulated. They can be used to enforce certain conditions, automate processes, and integrate with other systems.
Why is this an issue?
A Business Rule is designed to execute when a specific operation takes place on a table, one such operation being an update. Utilizing current.update()
triggers all "on-update" Business Rules linked to that table.
However, incorporating side effects in a Business Rule code is not recommended. As the system expands and the quantity of Business Rules increases, predicting the subsequent operations post current.update()
becomes progressively challenging.
While ServiceNow includes mechanisms to address and avoid potential infinite loops that could lead to instance crashes, this process is resource-intensive and impacts performance. This is particularly common when dealing with tables that go over numerous operations.
How do I fix it?
There are different scenarios in which you can update the current record without using current.update()
while simultaneously following the best practices in ServiceNow. The way to fix these issues will depend on the specific business rule you are working with:
- Before Business Rules: Never use
current.update()
in a Before Business Rule. ServiceNow automatically saves all values stored oncurrent
after the Business Rule is executed. The following code illustrates an error and the correct approach to update a field when a record updates:
Wrong:
1 2 3
current.name = "John Doe"; current.age = 31; current.update();
Correct:
1 2
current.name = "John Doe"; current.age = 31;
After Business Rules: Avoid using
current.update()
in an After Business Rule as it leads to your code being updated twice. The recommended solution for this issue is to transform the After Business Rule into a Before Business Rule, eliminating the need forcurrent.update()
.Async Business Rules: Because an async business rule runs after the data is modified in the database, its solution is the same as that of an After Business Rule.
Display Business Rules: Updating a record when a user opens it is not ideal for user experience. A less aggressive approach, such as using a Client Script, is a better choice.