Finding Duplicate Assets

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-27-2019 08:03 AM
I have a script include that I found in the community to assist in finding duplicate records in a table. In this case I am using it to locate duplicate assets on the alm_asset table. The script works as expected as it returns a list of duplicated records. So if I have 2 assets that are in the table twice the script return all 4 records. I want to add a line that returns one record so I can create a report showing the duplicated assets.
The script is:
function getDupes(alm_asset, asset_tag) {
var q = new GlideAggregate(alm_asset);
//q.addQuery('active', '=', 'true');
q.addAggregate('COUNT', asset_tag);
q.addHaving('COUNT', asset_tag, '>', '1'); (duplicates)
q.query();
var listOfDupes = new Array();
while (q.next()) {
listOfDupes.push(q.getValue(asset_tag));
}
return listOfDupes;
}
The above screenshot shows the returned list. This is from my PDI as I am testing the script..
thanks in advance for any assistance.
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-27-2019 08:09 AM
Hi Bowden,
use unique to get unique names
function getDupes(alm_asset, asset_tag) {
var q = new GlideAggregate(alm_asset);
//q.addQuery('active', '=', 'true');
q.addAggregate('COUNT', asset_tag);
q.addHaving('COUNT', asset_tag, '>', '1'); (duplicates)
q.query();
var listOfDupes = new Array();
while (q.next()) {
listOfDupes.push(q.getValue(asset_tag));
}
var arrayUtil = new ArrayUtil();
listOfDupes = arrayUtil.unique(listOfDupes);
return listOfDupes;
}
Mark Correct if this solves your issue and also mark Helpful if you find my response worthy based on the impact.
Thanks
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-27-2019 08:18 AM
Thanks for the quick reply, I updated the script with your addition and the unique values did not come up, I received the same returned list as previously noted.
oh and I removed the (duplicates) as it was a commented section that I did not remove all the way.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-27-2019 09:16 AM
Hi Bowden,
Did you try adding logs to check array before and after applying unique?
Mark Correct if this solves your issue and also mark Helpful if you find my response worthy based on the impact.
Thanks
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-27-2019 09:21 AM
not sure how that is done as I am a newbe when it comes to scripting. I found the script on another post in the community.