Christopher_Mal
ServiceNow Employee
ServiceNow Employee

I have been meaning to post this for a while now. Most of us are aware and use the dot walk method for dereferencing GlideRecord.

It goes something like this:



myFunction();

function myFunction() {
var gr = new GlideRecord('incident');
gr.addQuery('active', true);
gr.query();

while (gr.next()) {
gs.print(gr.sys_id);
}
}


There is a different way you can do it as well. By indexing the field name from an array you can also get values out.
Like this:


myFunction();

function myFunction() {
var gr = new GlideRecord('incident');
gr.addQuery('active', true);
gr.query();

var str = 'sys_id'; // <<<------ This can come from a database query

while (gr.next()) {
gs.print(gr[str] + ''); // <<<------ Notice the array index
}
}


Now I would continue doing it the familiar way (with dot walking), but you may have a use case one day to dynamically get data from a GlideRecord and all you have is a string value of the field to get. Dereferencing an array based on that field name index is probably gonna be a much easier solution to contrive. Cheers.

2 Comments