GenevaGem - Business Rule Evolution

As you can expect, platform technologies need to be constantly evolving to keep up with improvements in reliability, security, performance, and industry standards. ServiceNow scripting environment is no different. The Geneva release introduces a couple new features for business rule scripting. 

Here's a quick run through of the evolution of business rule scripts to help you understand the high-level changes on Fuji and Geneva.

Fuji

It didn't make the cut for it's own FujiForty post, but Fuji introduced a new default function structure for business rules. In pre-Fuji releases you could put whatever you wanted in the script field. You could define your own functions to invoke, or you could just write code outside of functions. This was nice and easy, but presented problems. Anything outside of a function was executed in the global context of the transaction. The primary issue occurs when you declare or update the value of a variable that's already defined, and expected in the global context (other scripts processed during the transaction). These aren't always easy to identify which is why it's been recommended that you always wrap your logic in a function and invoke it from the rule. This keeps the variables local to the function scope. Essentially you're "immediately invoking" your function, but protecting the variables used inside the function from impacting those in the global context. 

Any script allowed in pre-Fuji releases

When creating a new rule in Fuji, the default function names are defined by the "when" (onBefore, onAfter,...) value of the rule. These functions were also required. Creating a new rule without these functions would cause an error. However, you could modify exiting rules that were created before Fuji. If you do need a rule without these functions (there's only a few cases I know of), you can use the "insert" action to copy and edit an existing rule.

Fuji business rule required function structure

 

Geneva

Now that you're used to the Fuji format, it's time to make a change. The Geneva release introduces a couple major changes to business rules. 

IIFE 

Say what? It's pronounced "iffy". The first thing you'll notice when creating a rule in Geneva is the default scripts looks different. The new format is a commonly used standard referred to as an anonymous function, or IIFE (immediately invoked function expression). There's plenty of explanations on the what, why, and hows, so I won't get into the details. Here's a quick summary to get you started.

Geneva IIFE script structure

The IIFE format is pretty easy to follow. You no longer need to specify the "when" action as this is handled by the platform. The "current" and "previous" objects are just as they were before. So basically you can just add your logic in the function wrapper and you're good to go.

This is the new recommended standard, but unlike Fuji, it's not a restriction. You can still create new rules without an IIFE format. You'll also notice some left-over core rules using the Fuji format. These will still execute. You just won't be able to create and execute a new rule using the Fuji format. It's best to stick with the new standard.

 

Scripting assistance

The next new feature, and much more exciting is auto-complete scripting assistance. It's built into the script editor so it also works in script includes and other script fields. This is only available for the common system classes. Unfortunately there's no way to add support for your custom classes yet, but that sure would be handy. 

Geneva auto-complete example for "gs" object

For those of you building scoped applications, you'll be happy to hear that it appears that most legacy methods I tried do not show up in the auto-complete list.

List of system objects available

GlideRecord objects display methods and fields based on the current rule table

 

One final tip, really more of a reminder. Script macros are still available to use and customize. The default macros are listed below.

The Syntax Editor macros are:
-----------------------------
doc - Documentation Header 
for - Standard loop for arrays
vargror - Example GlideRecord Or Query
info - 
method - Standard JavaScript Class Method
vargr - A common pattern of creating and querying a GlideRecord