Skip to main content

What is Business Rules?

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,Business Rules,Type of  Business Rules
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,Business Rules,Type of Business Rules
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.

After Business Rule,Business Rule, Type of Business Rule
After Business Rule

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,Business Rule,Type of Business Rules
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 Rules,Type of Business Rules
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

Popular posts from this blog

What is ServiceNow?

What is ServiceNow? ServiceNow  is a cloud based ITSM tool which provides service management software as a service and greater flexibility in terms of functionality, customization, reliability etc. When it was first introduced it was an ITSM tool but now it more than just ITSM tool.  ServiceNow  also caters for  IT Operation Management ,  IT Business Management . It has robust cloud platform on top of which, any application can be built quickly. ServiceNow Platform is one of the biggest strengths of ServiceNow. What is ServiceNow? You can build a different type of application on this platform for HR, Finance, Security, Customer Service etc. This tool has already captured a great market share. Best thing is while implementing this tool in your organization you don’t need to think about infrastructure, servers, database, data centers, security etc all these are taken care by ServiceNow itself. you just need to focus on implementing the business requireme

What is Client Scripts?

What is Client Scripts - ServiceNow? Client scripts sits on the client side (the browser) and run there only instead of on the server. Client Scripts manage forms and fields in real time. Examples include:          Modify choice list options          Hide/Show form sections          Set one field in response to another in real time          Make fields mandatory          Hide fields Client Scripts are of 4 types: ServiceNow Client Scripts OnLoad():      Script runs when a form meeting the trigger condition loads and before control is given to user.        Typically used to manipulate a forms appearance or content.        The onLoad() function template is automatically inserted in the Script field.        The OnLoad function has no argument passed to it. OnSubmit():        Script runs when a form meeting the trigger condition is saved, updated, or submitted.        Typically used for field validation.        The onSubmit() functi