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  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

View solution in original post

10 REPLIES 10

Maik Skoddow
Tera Patron
Tera Patron

Hi @Prasanthi ,

as you can see on page https://developer.servicenow.com/dev.do#!/reference/api/quebec/server_legacy/c_GlideRecordAPI there is no official method .getElements() for class GlideRecord.

What did you expect to get?

Kind regards
Maik

If my answer replied your question please mark appropriate response as correct so that the question will appear as resolved for other users who may have a similar question in the future.

Prasad Dhumal
Mega Sage

You are using .next method, there is no need to use this Method.

next() method returns the next element and also moves to the next element.

 

Try below code into Background Script, you will get Output.

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

Please mark my answer as correct and close the thread so that others can also benefit.

Regards

Prasad Dhumal

Prasanthi1
Giga Contributor

Here my question is, while printing length of array, I am not getting the length.

Is there any error in my code... please explain my code only.

 

Thank you

 

@Prasanthi 

Did you get a chance to check my comments above

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

Regards
Ankur

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

Actually getElements() methods returns array of objects, then why we agiain convert this to array?