Global variables in business rules

Predefined global variables are available for use in business rules.

Use the following predefined global variables to reference the system in a business rule script.

Global variable Description
current The current state of the record being referenced. Check for null before using this variable.
previous The state of the referenced record prior to any updates made during the execution context, where the execution context begins with the first update or delete operation and ends after the script and any referenced business rules are executed. If multiple updates are made to the record within one execution context, previous will continue to hold the state of the record before the first update or delete operation. Available on update and delete operations only. Not available on asynch operations. Check for null before using this variable.
g_scratchpad The scratchpad object is available on display rules, and is used to pass information to the client to be accessed from client scripts.
system (or gs) References to GlideSystem functions.

The variables current, previous, and g_scratchpad are global across all business rules that run for a transaction.

Prevent null pointer exceptions

In some cases, there may not be a current or previous state for the record when a business rule runs, which means that the variables will be null. To check for null before using a variable, add the following code to your business rule:
if (current == null) // to prevent null pointer exceptions.
return; 

Define variables

User-defined variables are globally scoped by default. If a new variable is declared in an order 100 business rule, the business rule that runs next at order 200 also has access to the variable. This may introduce unexpected behavior.

To prevent such unexpected behavior, always wrap your code in a function. This protects your variables from conflicting with system variables or global variables in other business rules that are not wrapped in a function. Additionally, variables such as current must be available when a function is invoked in order to be used.

The following script is vulnerable to conflicts with other code. If the variable gr is used in other rules, the value of the variable may unexpectedly change.
var gr = new GlideRecord('incident');
gr.query(); 
while(gr.next()) {
 
   //do something
 
}
When this script is wrapped in a function, the variable is available only within the function and does not conflict with other functions using a variable named gr.
myFunction();
 
function myFunction() { 
  var gr = new GlideRecord('incident');
  gr.query(); 
  while(gr.next()) { 
    //do something 
} }