- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-08-2022 06:06 AM
I expected this small piece of code to show the value from this iteration and the previous
var ce = new GlideAggregate('cert_element');
count = 0;
ce.addAggregate('COUNT', 'configuration_item');
//ce.addQuery('sys_created_on', '>=', '2022-12-05');
ce.orderBy('configuration_item');
ce.orderBy('state');
ce.groupBy('configuration_item');
ce.query();
pre = '';// Set prevous value to nothing.
while(ce.next()){
gs.print('Before -- Now: ' +ce.configuration_item.name+ '\tThen: ' +pre.configuration_item.name); // Properly shows the first value for Now and undefined for Then but only for the first time. Every time after it shows the same value.
pre = ce; // Set pre to cs and on the start of the next iteration, pre should be what cs is now and cs should change to the new value.
gs.print('After -- Now: ' +ce.configuration_item.name+ '\tThen: ' +pre.configuration_item.name); // Properly shows the same value because the new value was set to the old.
count++;
}
gs.print(count);
The first time through, it shows ce as the new value and pre as nothing (undefined), which is right for "Before".
It then updates pre to ce and they show the same thing, which is right for "After".
The second time through, it shows ce as the new value of ce for ce and that same new value for pre. I expected the value of pre to still be set to the previous value of ce until after it prints the "Before".
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-08-2022 06:17 AM
I believe this has to do with "pre" pointing at the same GlideRecord object each time. Do you need access to the entire GlideRecord object? Can you do something like the below code?
var ce = new GlideAggregate('cert_element');
count = 0;
ce.addAggregate('COUNT', 'configuration_item');
//ce.addQuery('sys_created_on', '>=', '2022-12-05');
ce.orderBy('configuration_item');
ce.orderBy('state');
ce.groupBy('configuration_item');
ce.query();
pre = {name: ""};
while(ce.next()){
gs.print('Before -- Now: ' +ce.configuration_item.name+ '\tThen: ' +pre.name); // Properly shows the first value for Now and undefined for Then but only for the first time. Every time after it shows the same value.
pre.name = ce.configuration_item.name.toString() // Set pre to cs and on the start of the next iteration, pre should be what cs is now and cs should change to the new value.
gs.print('After -- Now: ' +ce.configuration_item.name+ '\tThen: ' +pre.name); // Properly shows the same value because the new value was set to the old.
count++;
}
gs.print(count);

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-08-2022 06:17 AM
I believe this has to do with "pre" pointing at the same GlideRecord object each time. Do you need access to the entire GlideRecord object? Can you do something like the below code?
var ce = new GlideAggregate('cert_element');
count = 0;
ce.addAggregate('COUNT', 'configuration_item');
//ce.addQuery('sys_created_on', '>=', '2022-12-05');
ce.orderBy('configuration_item');
ce.orderBy('state');
ce.groupBy('configuration_item');
ce.query();
pre = {name: ""};
while(ce.next()){
gs.print('Before -- Now: ' +ce.configuration_item.name+ '\tThen: ' +pre.name); // Properly shows the first value for Now and undefined for Then but only for the first time. Every time after it shows the same value.
pre.name = ce.configuration_item.name.toString() // Set pre to cs and on the start of the next iteration, pre should be what cs is now and cs should change to the new value.
gs.print('After -- Now: ' +ce.configuration_item.name+ '\tThen: ' +pre.name); // Properly shows the same value because the new value was set to the old.
count++;
}
gs.print(count);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-08-2022 09:41 AM
I am "building" this report one step at a time. What you provided gives me exactly for what I was looking. I do need more of that glide record to answer your question. My goal is to compare the status of that record and then add a "priority" to the state. So if the highest state is Failed, second highest is Pending and least is Certified. I want to end up with a report that represents:
- If the ci has any element that has failed, the ci is failed.
- If the ci doesn't have any failed elements but has some pending, the ci is pending.
- Else the ci is certified.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-08-2022 10:01 AM
Are you able to just expand on this code to add more fields? You might try creating a function to return the GlideRecord itself, or stringifying it and then parsing it later.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-08-2022 11:30 AM
Thanks. I will try that. If I fail, I'll create a new post because you answered this one. Thanks for that.