Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

How to retrieve the list of records through gliderecord?

Not applicable

Requirement: I need to retrieve the list of records through Gliderecord. I used below code but it is only retrieving the first record in the list.

1 ACCEPTED SOLUTION

Mohith Devatte
Tera Sage
Tera Sage

Hello 

please change the if to while

also if you want all names like this

var arr=[];

var obj2 = new GlideRecord('cmdb_rel_ci');

obj2.addEncodedQuery('yourencodedquery');

obj.query();

while(obj.next())

{

arr.push(obj2.child.getDisplayValue());

}

gs.info('My object names'+arr.toString)

PLEASE MARK MY ANSWER CORRECT IF IT HELPS YOU

View solution in original post

10 REPLIES 10

Hmm strange .check the row count in the table please?

checked, there are 7 records in the table by applying filter which i have used in encodedquery.

There was silly mistake in your code, i correct it  now it's working. Thank you so much.

gs.info('My object names'+arr.toString());

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

are you sure there are more than 1 records satisfying your query?

try adding this to get row count gs.info(obj2.getRowCount());

Regards
Ankur

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

asifnoor
Kilo Patron

Hi,

Not sure if this is the complete code.

1. I see sysid2 is not defined here. 

2. Declare names as array outside while loop and push the values into the array.

3. Your return shoudl be outside of while loop (change if to while). 

Also, as others suggested do chck if you actually have more rows using getRowCount().

Mark the comment as a correct answer and also helpful if this has helped to solve the problem.