What is Business Rules - ServiceNow?
Business Rule is server side scripting that executes whenever a record is inserted, updated, deleted, displayed or queried.
The
key thing is to keep in the mind while creating a business rule is that
when and on what action it has to execute. You can run the business
rule ‘on display’, ‘on before’ or ‘on after’ of an action (insert,
delete, update) is performed. It executes on the server and does not monitor form fields.
When to run Business Rules:
1) Display :
- Runs when the form loads.
- Primary purpose is to populate the g_scratchpad global object.
- Use to provide client-side scripts access to server-side data
Display Business Rules |
2) Before :
- Executes after form submission and before the record update in the database.
- Use
to update information on the current object. Example, a Business Rule
containing current.state=3; would set the state field on the current
record to the state with a value of 3.
Before Business Rule |
3) After :
- Executes After form submission and after the record update in the database.
- Use to update information on related objects that need to be displayed immediately, such as GlideRecord queries.
4) Async:
- Executes after records are inserted/modified/queried.
- Run asynchronously.
- Use to update information on related objects that do not need to be displayed immediately, such as calculating metrics and SLA’s
Async Business Rule |
Realtime examples of Before, After, Async and Display Business Rules:
Let's say you are posting a question on community and community is your ServiceNow an instance.
First,
I need to check that you not posting any offensive materials. I can
have a before rule to check that. According to the rule set up by me,
either allow or disallow the posting of the question (Transaction).
Second,
after you have passed the rules of posting and posted the question
successfully. Someone opens up your question and likes your question. I
need to award you points once the like is successful. I can have an
after business rule to set this up.
Third,
you will need to get email trigger when someone comments on your
question. It does not need to be immediate. I can have an async rule to
trigger the email.
Finally,
I need to update the number of views of your question. I can have a
display business rule to update the counter related to your question.
Business Rule Process Flow:
Business Rule Process Flow:
Business Rule Process Flow |
Business Rules Best Practices:
**Prevent Recursive Business Rules:
- Do not use current.update() in a Business Rule script.
- The
update() method triggers Business Rules to run on the same table for
insert and update operations, potentially leading to a Business Rule
calling itself over and over.
- Changes
made in before Business Rules are automatically saved when all before
Business Rules are complete, and after Business Rules are best used for
updating related, not current, objects.
- When a recursive Business Rule is detected, ServiceNow stops it and logs the error in the system log.
- However, this behavior may cause system performance issues and is never necessary.
**Use Script Includes Instead of Global Business Rule:
- A global Business Rule is any Business Rule where the selected Table is Global.
- Global Business Rule has no condition or table restrictions and loads on every page in the system.
- There is no benefit to loading this kind of script on every page. The script includes only load when called.
- If you have already written a global business rule, move the function definition to a Script Include.
- The name of the Script Include must match the name of the function for the Script Include to work properly.
- There is no need to modify any calls to the named function.
**Keep Code in Functions:
- By default, an advanced Business Rule will wrap your code in a function, and it is important that this guideline is followed.
- When the code is not enclosed in a function, variables and other objects are available to all other server-side scripts
- This availability can lead to unexpected consequences that are difficult to troubleshoot.
Debugging Business Rules:
**Send yourself information
- gs.log(): writes to the system log file, log table and Debug Business Rule
- gs.logError(): writes an error message to the system log
- gs.logWarning(): writes a warning message to the system log
- gs.print(): writes to the system log file and Debug Business Rule area
**More ways to send yourself information
- gs.addInfoMessage(): outputs a blue info message to the top of a form
- gs.add ErrorMessage(): outputs a red error message to the top of a form
What is Client Scripts - ServiceNow?
Comments
Post a Comment