Help
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Jim Coyne
Kilo Patron
Part of the Tips 'N Tricks" series.

 

I keep seeing a particular problem with people posting questions about data issues when using Arrays.  And to be clear, it's not really an Array issue, but a problem with how data is retrieved from the GlideRecord object.

 

Here's an example to illustrate the problem.  The following 5 Incident records are in my personal development instance that has some OOB demo data:

 

find_real_file.png

 

Notice the Priority column.  Let's write a script that will grab the value of Priority for each of the records:

 

(function() {
    var log = [];
    var gr = new GlideRecord("incident");
    gr.addActiveQuery();
    gr.orderByDesc("number");
    gr.query();
    while (gr.next()) {
        log.push(gr.priority);
    }
    return log;
})();

 

Looking at the code, you would assume it's going to return an Array with the following results:

3 (Moderate)
5 (Planning)
5 (Planning)
1 (Critical)
1 (Critical)

However, the results are quite different:

 

find_real_file.png

 

What, that's not right.  Is it?  Why are they all 1?

 

The problem with the code is...

 

log.push(gr.priority);

 

...is putting the GlideElement object (in this case a GlideObjectNumeric because the field is an Integer) into the Array instead of the actual "value" of the field, so it changes as you loop through the records, becoming the value of the next record as you loop.  To add the real "value" of the field to the Array, use this instead:

 

log.push(gr.getValue("priority"));

 

The "getValue()" method returns a string representation of the data in the field.  This is the safest way to ensure you are putting the proper data into the Array, or any other variable.  The issue is found more often in code when data is pushed into an Array because you are typically looping through multiple records, hence changing the "view" of the data, but it can happen anywhere.

 

So if we change the code a bit:

 

(function() {
    var log = [];
    var badWay = [];
    var properWay = [];
    var gr = new GlideRecord("incident");
    gr.addActiveQuery();
    gr.orderByDesc("number");
    gr.query();
    while (gr.next()) {
        badWay.push(gr.priority);
        properWay.push(gr.getValue("priority"));
    }
    log.push(badWay);
    log.push(properWay);
    return log;
})();

 

...and re-run it, the results are quite different:

 

find_real_file.png

 

Notice how the values of the badWay Array all contain "1" but properWay contains the correct values.  If we change the code again to:

 

(function() {
    var log = [];
    var badWay = [];
    var gr = new GlideRecord("incident");
    gr.addActiveQuery();
    gr.orderByDesc("number");
    gr.query();
    while (gr.next()) {
        badWay.push(gr.priority);
        log.push(badWay.toString());
    }
    return log;
})();

 

...adding log.push() within the while loop shows what is happening to the Array as we loop through each record:

 

find_real_file.png

 

Each time the "badWay.push(gr.priority);" is called, we are adding a new pointer to the "current" record into the Array.  Because the Array contains pointers to the current record, the values all change to that current value as we loop.

 

So changing the code one last time shows the data being added to the Array properly as we loop:

 

(function() {
    var log = [];
    var properWay = [];
    var gr = new GlideRecord("incident");
    gr.addActiveQuery();
    gr.orderByDesc("number");
    gr.query();
    while (gr.next()) {
        properWay.push(gr.getValue("priority"));
        log.push(properWay.toString());
    }
    return log;
})();

 

find_real_file.png

 

I hope I made it pretty clear why we should be using "getValue()" when getting data from a GlideRecord object.

 

NOTE: "getValue()" does not work when dot-walking along reference fields, so you will want to use "toString()" instead to force the value to a string (e.g. gr.caller_id.department.toString() to get the sys_id of the User's Department).

2 Comments