- 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 03:25 PM
Hello,
So you're basically needing arrays inside of an object?
If so, then you'd create an array inside the while loop, fill it, then push the array to the object.
Also, see this support article as well: https://support.servicenow.com/kb?id=kb_article_view&sysparm_article=KB0858268
So you should be using getValue(), especially within a while loop.
Please mark reply as Helpful/Correct, if applicable. Thanks!
Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-05-2021 03:32 PM
basically what I'm trying to do is creating an array [] and inside that array there are JSON objects

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-05-2021 03:33 PM
Hi,
Please review my reply above, thanks!
Please mark reply as Helpful/Correct, if applicable. Thanks!
Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-05-2021 03:40 PM
I tried the reply above but it's the opposite way of what I'm trying to do and giving me an empty object instead. I'm looking for single array and inside that array multiple objects