Business Rules
From SNCWiki
| Business Rules |
|---|
| Functionality described here requires the Admin role. |
| Note: This functionality requires a knowledge of Javascript. |
Contents |
Overview
A business rule is a piece of JavaScript configured to run when a record is inserted, updated, or deleted, or when a table is queried. A business rule can be set to run before or after the database action has occurred. In the case of a query, the business rule should run before the database operation, so that the data returned to the user is appropriate to his system privileges (user roles). A typical business rule might execute a script after a user updates an incident or escalates the priority of a change request. You use the business rules form to create new events for email notification and script actions.
This is an example of a business rule:
This example is the business rule used for saving updates made to the change_phase table. The rule is configured to execute the JavaScript code after an insert or update is made on the change_phase table.
| Field | Input Value |
| Name | A descriptive name for your business rule. |
| Table | Select the appropriate database table for this business rule. |
| Order | Type the sequence in which this business rule should run. If there are multiple rules on a particular activity, then the rules will run in the order specified here, from lowest to highest. |
| Active | Select the check box (true) to enable this business rule. |
| When | Select when this business rule should execute: before, async(at the same time), or after the database operation is complete. Your system may have legacy selections from builds prior to the Winter 2008 release, such as:
These selection were converted to UI Actions in the Winter 2008 release. |
| Insert | Select this checkbox to execute the business rule when a record is inserted into the database. |
| Update | Select this checkbox to execute the business rule when a record is updated. |
| Delete | Select this checkbox to execute the business rule when a record is deleted from the database. |
| Query | Select this checkbox to execute the business rule when a table is queried. |
| Condition | Create a statement for a condition under which the business rule should execute. By adding the condition statement to this field, you tell Service-now to evaluate the condition separately and parse the script only if the condition is true. If you decide to include the condition statement in the script, leave this field blank. |
| Script | Create a script that triggers your new event when the condition you define is true. |
Business Rule Scripting
You create scripts in business rules with JavaScript. The following are the predefined variables that help reference the system:
- current: The current record being referenced
- previous: The record before any changes were made
- system (or gs): References system functions
Variables, such as current and previous, that you declare in a business rule may be global variables and might get overwritten unexpectedly during the processing of the business rule due to other business rules or other code running at the same time. To avoid this, it is good practice to wrap the code of your business rule in a function, and then call that function. By doing this, the variables you use inside the function have local scope and do not conflict with other code being executed simultaneously.
This is an example of a script that is vulnerable to conflicts with other code.var gr = new GlideRecord('incident'); gr.query(); while (gr.next()) { //do something }
When this script is wrapped in a function, it cannot be overwritten.
myFunction(); function myFunction() { var gr = new GlideRecord('incident'); gr.query(); while (gr.next()) { //do something } }
Global Business Rules
Business Rules marked as running on table Global are loaded and initialized at the beginning of each interaction between a user and the platform. Global business rules ignore the Condition field. Therefore, unless the script is wrapped in a function, it will run on every interaction between the user and the platform.
Global Business Rules that have script wrapped in a function can be called upon by any script running elsewhere. Client scripts and business rules can reference any function defined in a global business rule. Global business rules are therefore useful in defining functions to be used repeatedly by a variety of scripts.
Before vs. After Business Rules and current.update()
The function current.update() is unnecessary in a business rule that executes either before or after the database activity. Use of this function causes double updates and therefore double events. All changes to the current record should be made in before business rules, which then lets the system update the record. After business rules should only react to the update to create events, update related records (like the incidents related to an updated change), etc.
Example: Lock Accounts That Don't Meet Role or Active Requirements
// Lock accounts if bcNetIDStatus != active in LDAP and user does not // have self-service, itil or admin role var rls = current.accumulated_roles.toString(); if ( current.u_bcnetidstatus == 'active' && ( rls.indexOf(',itil,') > 0 || rls.indexOf(',admin,') > 0 || rls.indexOf(',ess,') > 0 ) ) { current.locked_out = false; } else { current.locked_out = true; } var gr = new GlideRecord ("sys_user"); gr.query(); while(gr.next()) { gr.update(); gs.print("updating " + gr.getDisplayValue()); }

