Duplicate serial number assets report

_bhishek
Tera Guru

Hi All,

I am able to find the duplicate serial number through background script. I want to create a report with list view in service now  for the duplicate serial number assets(alm_asset table).

Could you please help me how can i do that .I am using below script.

var gaDupCheck = new GlideAggregate('alm_asset'); 


gaDupCheck.addAggregate('COUNT', 'serial_number'); 
gaDupCheck.addNotNullQuery('serial_number'); 
gaDupCheck.groupBy('serial_number'); 
gaDupCheck.addHaving('COUNT', '>', 1); 
gaDupCheck.query(); 

while (gaDupCheck.next()) { 

 var gr = new GlideRecord('alm_asset');
  gr.addQuery('serial_number',gaDupCheck.serial_number);
  gr.query();
  gs.info('Duplicate serial number: ' + gaDupCheck.serial_number);

}
1 ACCEPTED SOLUTION

kps sumanth
Mega Guru

Hello @_bhishek ,

 

You can create a script include for the same and you can use that script include and use the same in your report.

Create a script include:

var Duplicate_Records_serial = Class.create();
Duplicate_Records_serial.prototype = Object.extendsObject(AbstractAjaxProcessor, {
duplicateRecord: function(){
	var numbers = [];
	var gaDupCheck = new GlideAggregate('alm_asset'); 


gaDupCheck.addAggregate('COUNT', 'serial_number'); 
gaDupCheck.addNotNullQuery('serial_number'); 
gaDupCheck.groupBy('serial_number'); 
gaDupCheck.addHaving('COUNT', '>', 1); 
gaDupCheck.query(); 

while (gaDupCheck.next()) { 
numbers.push(gaDupCheck.serial_number.toString());

//  var gr = new GlideRecord('alm_asset');
//   gr.addQuery('serial_number',gaDupCheck.serial_number);
//   gr.query();
//   gs.info('Duplicate serial number: ' + gaDupCheck.serial_number);

}
//gs.info("Serial_records "+numbers)
return numbers;
},

    type: 'Duplicate_Records_serial'
});

kps2_0-1707919941024.png

and create a report in that call the above script include like this.

kps2_1-1707919986828.png

Condition: serial number is "javascript:new Duplicate_Records_serial().duplicateRecord()"

Please mark this Accepted and helpful if this worked for you.

View solution in original post

7 REPLIES 7

Community Alums
Not applicable

I ran into the same issue and it was the condition on the report that was incorrect for me. Try making sure that the condition is:

 

javascript:new Duplicate_Records_serial().duplicateRecord()

 

 

Also, row 9 on the script include should filter out null serial numbers, so it may not be working correctly for you. 

 

A way to test if the script is working correctly is to create two assets with the same serial number and check if they show up in the report.

 

Edit: The report condition breaks when posting as text but it is: 

DylanBlumenber_0-1718896288809.png

 

Thank you so much Dylan.  Your suggestions proved most helpful.  I had to hand type the javascript as I was doing a copy and paste before and though it looked right, I guess it wasn't.  I've been able to pull the duplicate records for both asset tag and serial numbers using separate script includes and reports.  Yay!  Happy Dance! 😀

Joel O
Mega Sage

Stumbled upon this thread and I too am getting the blanked records instead of the non-blanked ones. Followed the instructions provided and ensured I typed out the condition in the field instead of copy and paste. Have applied this both to my DEV and PDI instances that have 100% duplicate serial numbers on a few assets.

However I'm still only able to pull the blanked serial number records. Looking to see @lwhite how you were able to get it to work or if there was something you altered in the script include for yourself. 

 

Much appreciate any assistance.