logo

NJP

TNT: The Importance of Using "getValue()" when Getting Data from a GlideRecord

Import · Apr 10, 2020 · article

However, the results are quite different:

image

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:

image

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:

image

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;
})();

image

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).

Labels:

View original source

https://www.servicenow.com/community/developer-blog/tnt-the-importance-of-using-quot-getvalue-quot-when-getting-data/ba-p/2273338