How to Create an array of GlideRecord objects?

chethann
Giga Contributor

Hi All,

I'm tried to copy the GlideRecord objects to an Array, so i can iterate through the array in HTML(view).

sample code -

var getHelpSupp = [];
var grSupp = new GlideRecord('u_get_help_support');
grSupp.addQuery('u_test_territory', lnk);
grSupp.orderBy('u_display_order');
grSupp.query();


while(grSupp.next()) {
//var test1 = grSupp;
getHelpSupp.push(grSupp);
}

// testing the data in array
for(var j=0; j<getHelpSupp.length; j++) {
var test = getHelpSupp[j];
console.log(test.u_support_label.getDisplayValue());
}

 

The query working fine, It fetching the 4 records from the table, the array size also a 4. but the issue all 4 records in the array is same, all are pointing to last record iterating in while loop GlideRecord.

It's some issue in copying the object in while it seems.

Anyone has faced same issue and has the solution pls.

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

Hi Chethan,

Following script you can try for example for pushing objects

var arr = [];

var gr = new GlideRecord('incident');
gr.setLimit(5);
gr.query();
while(gr.next()){

var obj = {};
obj.number = gr.number.toString(),
obj.shortDescription = gr.short_description.toString();


arr.push(obj);
}

gs.print(JSON.stringify(arr));

Mark Correct if this solves your issue and also hit Like and Helpful if you find my response worthy based on the impact.
Thanks
Ankur

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

View solution in original post

8 REPLIES 8

Ankur Bawiskar
Tera Patron
Tera Patron

Hi Chethan,

Following script you can try for example for pushing objects

var arr = [];

var gr = new GlideRecord('incident');
gr.setLimit(5);
gr.query();
while(gr.next()){

var obj = {};
obj.number = gr.number.toString(),
obj.shortDescription = gr.short_description.toString();


arr.push(obj);
}

gs.print(JSON.stringify(arr));

Mark Correct if this solves your issue and also hit Like and Helpful if you find my response worthy based on the impact.
Thanks
Ankur

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

Hi Ankur,

Thanks for your answer, it's working now.

If u don't mind, can i know the what is issue. It didn't for me?

Hi Chethan,

The way it was pushed into the array seems to be something weird. Also you were pushing glide record object and not javascript object notation.

Regards

Ankur

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

Hi

how could we retrieve it in the client side?

here we sent a string to client side, it is an array of objects.

how the element of each object is accessible in client script