How to remove the duplicate values via script
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-05-2024 10:53 AM
Hi Team,
Am trying to print the parent incident number + count of child incidents to the respective parent incident. Below is my code, but if there is two child incidents in my list am getting the print two times, how to remove the duplicate entry.
Code
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-05-2024 11:08 AM - edited ‎09-05-2024 11:35 AM
You can simply add a check to not run the aggregate twice for the same parent incident:
var parInc = '';
var a = new GlideRecord('incident');
a.addEncodedQuery('parent_incident!=NULL');
a.query();
while (a.next()) {
if (parInc != a.parent_incident) {
parInc = a.parent_incident.toString();
var b = new GlideAggregate('incident');
b.addQuery('parent_incident', a.parent_incident);
b.addAggregate('COUNT');
b.query();
if (b.next()) {
gs.print(" Incident " + a.parent_incident.number + " count is " + b.getAggregate('COUNT'));
}
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-05-2024 11:16 AM
Hi @Roshini
You can try the below Script:
var agg = new GlideAggregate('incident');
agg.addEncodedQuery('parent_incident!=NULL');
agg.groupBy('parent_incident');
agg.addAggregate('COUNT');
agg.query();
var existingincidents = [];
while (agg.next()) {
var parentIncidentnumber = agg.parent_incident.number;
if (existingincidents.indexOf(parentIncidentnumber) === -1) {
var childCount = agg.getAggregate('COUNT');
gs.print("Incident " + parentIncidentnumber + " count is " + childCount);
existingincidents.push(parentIncidentnumber);
}
}
Thanks and Regards
Sai Venkatesh