Finding Duplicate Assets

AndyB5000
Mega Guru

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;
}

find_real_file.png

 

The above screenshot shows the returned list.  This is from my PDI as I am testing the script..

 

thanks in advance for any assistance.

 

 

 

14 REPLIES 14

Ankur Bawiskar
Tera Patron
Tera Patron

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

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

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.

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

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

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.