How do i retrieve glide record elements from array

rajendravunnam
Kilo Expert

Hello,

I am new to ServiceNow and Javascript. I am having hard time in retrieving glide records from array:

This is the code i have. I ran it as a background script.

I would like to retrieve the all the columns from the record. I tried in many ways, but the last statement always returns NaN.

var array1=new Array();

var gr = new GlideRecord('change_request');

//gr.addQuery('state', 'New');

gr.query();

gs.log("Row count=",+gr.getRowCount());

var x = new GlideRecord();

while(gr.next())

{

array1.push(gr.getDisplayValue().toString()); //I tried gr.number,gr.sys_id, gr.number.getDisplayValue().......

}

gs.log('array length',+array1.length);

gs.log('printing 4th element',+array1[3].number.getDisplayValue());//---> This is returning NaN

output:

32: Row count=

32: array length

NaN: printing 4th element

Any help is appreciated..

Thanks!

Raj

6 REPLIES 6

Hey thanks for responding ...it's returning [object Object] as output


You need to iterate to get the array element.


Yeah because there may be multiple object returns from that table that's what I did.


Each time creating object and while completion of one iteration setting those value to changeRequest , now push it to array object crList and for second iteration again it creates changeRequest object and push into the array object crList and the process goes on depends iteration on how many records it fetched on the basis of query you have passed.


Simple.



Anyways Thanks