Reference versus Value

Another unique property of server-side code, is the structure of a GlideRecord. A GlideRecord consists of field-value pairs; the name of the GlideRecord object property corresponds to the field name (i.e. gr.number refers to the number field of the corresponding record). However, on the client, these properties of the GlideRecord object are strings; whereas on the server, these properties refer to GlideElement objects.

Why is this important, you ask? To answer that question, we must first understand one of the quirks of JavaScript: Pass-by-reference. In JavaScript, a variable can hold one of two types of values: a primitive value, or a reference. A primitive value might be a string, a number, a boolean (or null or undefined).

Primitive values are stored on the stack, and are accessed directly by the variable. 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 what is important is that they're different. A variable in JavaScript does not technically contain an object, but rather 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, with a property: prop1, set to a value: val1.

On line two, we then declare child, and set it as a reference to the same object as parent. In doing this, we might think that we are setting it to an instance of parent, but that is not the case!

On line three then, we set prop1 in the child object to new value, and then finally on line four, we print out the same property of the parent object.

So, what prints out? Although one might expect the prop1 property of the parent object to remain unchanged, due to the fact that both variables are references to the same object, modifying child also modified parent! Thus, on line four, we print out the new value, new value.

This applies just the same to GlideRecord objects on the server, since their properties are not a primitive value like a string (as they are on the client), but GlideElement objects. Consider the following code:

var num; 
var gr = new GlideRecord('incident');
gr.setLimit(2);
gr.orderBy('number');
gr.query();
while (gr.next()) {
if (gr.number == 'INC0000001') {
num = gr.number;
}
}
gs.print(num);

Above, we set a limit of 2 records to return, and order them by number. This should give us INC0000001 and INC0000002. In this code, we've specifically set a condition so that we will only set the num value, if the number we get ends in 0001. However, the next time the loop is run, we change the value of the GlideElement object that resides at gr.number, which the variable num references. When we print the contents of num, we're really printing out the contents of gr.number at the moment we access it. Therefore, we print out INC0000002 instead of the otherwise expected INC0000001.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.222.137.240