- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-10-2015 04:03 AM
Hello all,
I'm after some advice on how to add incident objects to an array from a glide query. Or, I'd like to be told that this is a bad idea, if that's the case!
I can create an array to store the sys_id as a string. And then use that sys_id to later get the incident and manipulate it or lookup values etc.
But, I was wondering it's a better or worse practice to store the whole object in the array from the start and then avoid having to get the incident later.
The example code below doesn't work and I've got my head around why: inc gets updated as we loop through the query results, and each element of the array is holding a reference to whatever inc is referencing, so each element ends up with the last value. So my question is: is there a way to add a reference to the actual incident inc is referencing at the time (the whole incident, not just the sys_id)?
var arr = [];
var inc = new GlideRecord('incident');
inc.addQuery('active', true);
inc.query();
while (inc.next()){
arr.push(inc);
}
for (var i = 0; i < arr.length; i++){
gs.print('item ' + i + ' number is ' + arr[i].number);
}
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-10-2015 12:01 PM
Hello Jamie,
Thank you very much!
I'm very sorry, I completely missed that.
But good news, I think I found a way to do it. I tested this code and it seems to be working:
var gObjects= new GlideRecord('incident');
gObjects.addQuery('active', true);
gObjects.query();
var objArray = [];
while(gObjects.next()){
var newObj = {};
for (var key in gObjects) {
//copy all the fields
newObj [key] = gObjects[key];
}
objArray.push(newObj);
}
Thank you.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-10-2015 04:51 AM
Try the below code and let me know the outcome.
var arr = [];
var inc = new GlideRecord('incident');
inc.addQuery('active', true);
inc.query();
while (inc.next()){
arr.push(String(inc.number));
}
for (var i = 0; i < arr.length; i++){
gs.print('item ' + i + ' number is ' + arr[i]);
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-10-2015 05:18 AM
Hi Pradeep,
Thank you for your reply. This would work fine to print out the values, but it still only creates an array of incident numbers stored as strings. What I am trying to do is to store an array of incident objects. So that I could then do something like below, for instance to get the company name referenced on one of the incidents in the array:
gs.print('company associated with item ' + i + ' is ' + arr[i].company.name;
so that arr[i] is an actual incident, rather than just the incident number or sys_id stored as string.
If I store the sys_id in the array, I could later get the company name like this:
var myinc = new GlideRecord('incident');
myinc.get(arr[i]);
gs.print('company associated with item ' + i + ' is ' + myinc.company.name;
I'm trying to avoid the need for creating a new GlideRecord object from the sys_id stored in the array, each time I need to grab something from that record. But, it may well be that this is the best way to do it in anyway.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-10-2015 08:38 AM
Hello Jamie,
Use the same code that Pradeep Sharma provided.
Just change this line
arr.push(String(inc.number));
to
arr.push(inc);
if it works please mark Pradeep answer as correct.
Thank you.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-10-2015 11:17 AM
Hello Edwin,
First - congratulations on being top contributor. I hope you have been sent a prize
arr.push(inc) - this was the line I had originally in my code. In this case it doesn't work unfortunately, as we end up with an array where all elements contain a reference to the final value of inc.