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

The Machine
Kilo Sage

the comment about using a script include is probably best. 

Dr Atul G- LNG
Tera Patron
Tera Patron

Hi @_bhishek 

 

Create a report and group by Serial number. 

*************************************************************************************************************
If my response proves useful, please indicate its helpfulness by selecting " Accept as Solution" and " Helpful." This action benefits both the community and me.

Regards
Dr. Atul G. - Learn N Grow Together
ServiceNow Techno - Functional Trainer
LinkedIn: https://www.linkedin.com/in/dratulgrover
YouTube: https://www.youtube.com/@LearnNGrowTogetherwithAtulG
Topmate: https://topmate.io/atul_grover_lng [ Connect for 1-1 Session]

****************************************************************************************************************

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.

lwhite
Tera Expert

When I try the accepted solution (code is copied and pasted), I get a report that lists out all the records with an empty serial number and no other records, but I know there are other records that have actual values that are duplicated.  Any ideas on why that is or how to correct it?