The Now Platform® Washington DC release is live. Watch now!

Help
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
SlightlyLoony
Tera Contributor

In a conversation I had with a customer the other day, she asked me why she saw so many examples of business rules written in functions, like this:


doImportantStuff();

function doImportantStuff() {
var x = 15;
var y = 22.3;
var z = Math.pow(15, 22.3);
current.answer = z;
}


Well, leaving aside the questionable wisdom of ever raising 15 to the 22.3rd power, that's a good question — and the answer lies both in the nature of JavaScript and of the Service-now.com platform:


First, consider that same (useless looking) business rule written without using a function:

var x = 15;
var y = 22.3;
var z = Math.pow(15, 22.3);
current.answer = z;


Doesn't that look simpler and better, somehow? Well, not so fast, code-monkey.

First you need to understand something about JavaScript: when you declare (or assign) variables outside the context of a class or function that you wrote, those variables become properties on the GLOBAL object. This global object is something that every JavaScript environment has, by definition. In client-side JavaScript, the

window
object is the global object; in server-side JavaScript on the Service-now.com platform, the global object is just an ordinary JavaScript object, with no special features or properties. So that simpler-looking version of the business rule defines three new (maybe) properties on the global object:

x
,

y
, and

z
.

Now a characteristic of the Service-now.com platform comes into play. When executing business rules, all of the business rules that run on a particular table (including global business rules) share the same context. That is, they share the same global object. That means that if you have ten business rules on a table, and one of those business rules defines a property — that property is then visible in all of those business rules.

This broad visibilty of global properties can be very useful, as it allows business rules to share values or to call functions in each other. But it can also introduce very subtle and hard-to-find problems — and for that reason, you should never deliberately use this feature unless you really can't figure out another way to accomplish something.

Here's an example of how this visibility can get you into trouble. Imagine two business rules like this:

// business rule 1...
var x = 15;

// business rule 2...
if (current.name == 'magic value')
x = true;
current.description = '';
if (x)
current.magic = true;


If these two business rules were written by different people at different times, the person writing the second one probably has no idea that the first one sets the variable

x
. In the second business rule,

x
will always evauate to true, no matter what is in

current.name
— because

x
is a global property common to both business rules.

So back to the functions... By putting all your business rule logic inside a function that you call, the only global property your business rule will set is the property containing the function. If you then give your function a name that's unlikely to be duplicated, you won't have any problem with "collisions" between business rules...

2 Comments