The basics of Business Rule in ServiceNow
Business Rule 101
Intro
Before defining business rules, let's get the full picture of Scripting in SN.
Scripting in ServiceNow falls into two categories:
- Client-side scripting (i.e., client script, UI Policy, UI Action, etc.)
- Server-side scripting (i.e., business rule, script include, etc.)
Server-side scripts execute on the ServiceNow server or database.
In this article, the focus is mainly on Server-side scripting.
Examples of Server-side Scripting:
- Update record fields when a database query runs
- Set field values on related records when a record is saved
- Determine if a user has a specific role
- Send email notifications
- Compare two dates to determine which comes first chronologically
- Calculate the date when the next quarter starts
- Log messages
- Send REST messages and retrieve results
Now, let's dive deeper into business rules.
Business Rule
As mentioned above, business rules are server-side logic that executes when database records are queried, updated, inserted, or deleted. Business Rules (BRs) respond to database interactions regardless of access method: for example, users interacting with records through forms or lists, web services, or data imports (configurable). Business Rules do not monitor forms or form fields but execute their logic when forms interact with the database such as when a record is saved, updated, or submitted.
Types of Business Rules
- Before: before BR runs before the form is saved to the DB (i.e., whenever you update a record by assigning the assigned_to yourself, you get acknowledged by a message above the record), it’s also possible to add a message. You can also abort the BR from performing changes/actions by selecting the abort action select box
- After: after the changes are saved in the DB. Generally, after BR is used to update related records
- Async: async works in the same way as the after BR. Both types run in the background. However, async BR will create a scheduled job that executes the action in the background at a later time
- Display: it passes some actions from the DB to the client/UI
Important concepts
Current vs Previous
- Current: values as they exist in the runtime environment
- Previous: values for the record fields when they were loaded from the db & before any changes were made
Example:
- When scripting, use dot-walking to retrieve or set field values on related
- records. The syntax is:
- <object> .< related_object> .< field_name>
For example:
if(current.u_requested_for.email == "beth.anglin@example.com"){
//logic here
}
You can also do that via the toggle script tree:
That's a wrap!
For practicing business rules, I recommend this tutorial.
References:
- https://www.youtube.com/watch?v=KRWdCFFfEek&ab_channel=ServiceNowDemo
- https://www.servicenow.com/community/itsm-forum/types-of-business-rules-with-example/m-p/712624
Comments
Post a Comment