Built something you're proud of? Tell the story. A quick G2 review of App Engine or Build Agent helps other developers see what's possible on ServiceNow. Share your experience.

length of an array

Prasanthi1
Giga Contributor

while executing the below, I am not getting the length of list.

var inc = new GlideRecord('incident');
inc.addActiveQuery();
inc.query();
inc.next();
var list = inc.getElements()
gs.print(list.length);

showing output: undefined

why? please explain..

1 ACCEPTED SOLUTION

Seems the docs has incorrect information.

I tried the script from docs to get the length but it failed

We need to convert that to Array and then get the length

Regards
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 10x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

View solution in original post

10 REPLIES 10

hvrdhn88
Giga Patron

try now

 

var inc = new GlideRecord('incident');
inc.addActiveQuery();
inc.query();
gs.print(inc.getRowCount());

hvrdhn88
Giga Patron

Ankur Bawiskar
Tera Patron

Hi,

getElements() would give you java object so convert that to Array and then use length

Script: This will give the length

var inc = new GlideRecord('incident');
inc.addActiveQuery();
inc.query();
inc.next();
var list = new global.ArrayUtil().convertArray(inc.getElements());
gs.info(list.length);

Output:

find_real_file.png

Regards
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 10x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

hvrdhn88
Giga Patron

Note: getElements() does not give list of rows details. It will return an array of GlideElement objects. Each object describes a field in the current GlideRecord.

 

eg:

 

This example retrieves a record from the incident table and shows the names of the elements (fields) in the record.