- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-05-2021 03:22 PM
Hello, This is my script to get all the incident records which are created today or yesterday, I'm getting the data but the problem is array.push is replacing my old data with a new one and only keeping the new one in response. I have tried to stringify it in many ways still keep the new data only. Please can someone guide me on how to overcome this problem and get the multiple records in an array or work out it will be very helpful.
var body = {};
var i=0;
var grInc = new GlideRecord('incident');
grInc.addEncodedQuery('sys_created_onONToday@javascript:gs.beginningOfToday()@javascript:gs.endOfToday()^ORsys_created_onONYesterday@javascript:gs.beginningOfYesterday()@javascript:gs.endOfYesterday()');
grInc.query();
while (grInc.next()) {
body.number = grInc.number;
body.short_description = grInc.short_description;
body.category = grInc.category;
body.caller_id = grInc.caller_id;
body.state = grInc.state;
body.assignment_group = grInc.assignment_group;
body.assigned_to = grInc.assigned_to;
result.push(body);
}
response.setBody(result);
Solved! Go to Solution.
- Labels:
-
Script Debugger
-
Scripting and Coding

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-05-2021 10:22 PM
Hello.
When you loop through a GlideRecord response the variable, in this case grInc, is just a pointer to the current record in the loop hence why you are only getting a single object in your array. You need to get the string value of the pointer using either getValue() or toString() when setting the properties of the object. So update your code to the following:
var i=0;
var grInc = new GlideRecord('incident');
grInc.addEncodedQuery('sys_created_onONToday@javascript:gs.beginningOfToday()@javascript:gs.endOfToday()^ORsys_created_onONYesterday@javascript:gs.beginningOfYesterday()@javascript:gs.endOfYesterday()');
grInc.query();
while (grInc.next()) {
var body = {};
body.number = grInc.number.toString();
body.short_description = grInc.short_description.toString();
body.category = grInc.category.toString();
body.caller_id = grInc.caller_id.toString();
body.state = grInc.state.toString();
body.assignment_group = grInc.assignment_group.toString();
body.assigned_to = grInc.assigned_to.toString();
result.push(body);
}
response.setBody(result);
Hope this helps.
--David
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-05-2021 04:34 PM
The line
var body = {};
should be inside the while() loop.
Vinod Kumar Kachineni
Community Rising Star 2022

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-05-2021 10:22 PM
Hello.
When you loop through a GlideRecord response the variable, in this case grInc, is just a pointer to the current record in the loop hence why you are only getting a single object in your array. You need to get the string value of the pointer using either getValue() or toString() when setting the properties of the object. So update your code to the following:
var i=0;
var grInc = new GlideRecord('incident');
grInc.addEncodedQuery('sys_created_onONToday@javascript:gs.beginningOfToday()@javascript:gs.endOfToday()^ORsys_created_onONYesterday@javascript:gs.beginningOfYesterday()@javascript:gs.endOfYesterday()');
grInc.query();
while (grInc.next()) {
var body = {};
body.number = grInc.number.toString();
body.short_description = grInc.short_description.toString();
body.category = grInc.category.toString();
body.caller_id = grInc.caller_id.toString();
body.state = grInc.state.toString();
body.assignment_group = grInc.assignment_group.toString();
body.assigned_to = grInc.assigned_to.toString();
result.push(body);
}
response.setBody(result);
Hope this helps.
--David