Avoiding Pass-By-Reference Using getValue() & setValue()

Server-side GlideRecord objects contain an element for each field in the table that the record is in. These elements are not primitive values (strings, numbers, booleans), but Objects. The type of object is GlideElement, and the GlideElement API is available. However, since JavaScript is a "loosely typed" language, you can reassign a variable of one data-type (such as an Object) to another type (such as a string) without any hassle. Thus, setting a GlideRecord (gr) element's value to a string will overwrite the reference to the GlideElement object itself. This bad-practice example would look like:

gr.short_description = 'New short desc.';

Primitive values are stored on the stack, and are accessed directly by variable name. You might say that they are "contained within" a variable. A reference value on the other hand, is merely a pointer (or, you might say, a reference!) to an object which is stored on the heap. The difference between the heap and stack isn't important here, but it is important is that they're different. An object variable in JavaScript does not technically contain an object, but rather it contains a reference to that object. Consider what might print out, if we run the following in a background script:

var parent = {
prop1: 'val1'
};
var child = parent;
child.prop1 = 'new value';
gs.print(parent.prop1);

In the above example, we declare an object: parent...

Read more

"Learning ServiceNow" is Now Available for Purchase!

We are pleased to announce that our new book, Learning ServiceNow, has been published and is available to purchase on Amazon, as well as on the publisher's website. Simply navigate to book.snguys.com to pick up a copy for yourself!

This book is geared toward new ServiceNow administrators and developers, who have less than a few years of experience managing the platform, but everyone should get something from it.
We take a ground-up approach in this book, and include advanced pro-tips along the way. We even have a whole section dedicated to things that expert developers wish they'd known when they were first starting out. There's something for everyone! 

All you need to know to get started, is basic JavaScript syntax, so head over to book.snguys.com and get your learn on!