Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

how can show duplicate entries in report

Deepthi13
Tera Expert

how can show duplicate entries in computer table for particular field.

i want to show duplicate entries  in report , please help

1 ACCEPTED SOLUTION

Priyanka Pednek
Kilo Guru

Hi Deepthi,

 

A script is the way to go on this one.   I rigged this duplicate finder up.   Feel free to alter it for your needs.   I ran this as a background script in my instance with out issue (or results, we don't have any dupes!).

 

 //highlight duplicate users
var arr = [];
var users = new GlideRecord('sys_user');
      users.addActiveQuery();
      users.query();
      while(users.next()) {
            arr.push(users.name.toString());
      }
     
//sort array by name, duplicate name are placed next to each other in the array
      arr.sort(function(a,b) {  
          return a<b ? 1 : a>b ? 1 : 0; });
                                                       
//remove duplicates:
    for(var j=0, len = arr.length, count=0, dupes=[]; j != len; j+=1) {
            if ( arr[j] == arr[j+1] ) {//if the name is the same as the name in the next arr index
                  ++count;
                  dupes.push(arr[j]);
            }
    }
//Output:
    gs.log("There are " + count + " duplicate users detected.\nUsers found: ")
    for (var k=0; k!=dupes.length;k+=1) {
    gs.log(dupes[k]);
    }

 

You can find below link that may help you to understand .

https://community.servicenow.com/community?id=community_question&sys_id=94ce0b61dbdcdbc01dcaf3231f96...

Mark Correct if this solves your issue and also mark Helpful if you find my response worthy based on the impact.
Thanks Deepthi

 

 

 

View solution in original post

3 REPLIES 3

Community Alums
Not applicable
Hi Deepthi, You need to create a Script Include and call it in the report. Script Include: Function getDupes(){ var dupRecords = []; var gaDupCheck1 = new GlideAggregate('cmdb_ci'); gaDupCheck1.addQuery('active','true'); gaDupCheck1.addAggregate('COUNT', 'asset_tag'); gaDupCheck1.groupBy('asset_tag'); gaDupCheck1.addHaving('COUNT', '>', 1); gaDupCheck1.query(); while (gaDupCheck1.next()) { dupRecords.push(gaDupCheck1.user_name.toString()); } return dupRecords; } For calling script include in report, refer to the following article. https://community.servicenow.com/community?id=community_article&sys_id=bf7f9320dbf7ab40fece0b55ca9619b1 Please mark this comment and article as correct answer/helpful if it helped you. Cheers, Hardit Singh

Hi Hardit Singh,

Thanks for your input.

i tried in following way as per your suggestion but when i RUN report 0 records where displaying, when i run same script in background script i am getting desired out put after printing. Please suggest.

please find my screen shot and script as below

Script Include: 

function getDupes(){
var dupRecords = [];
var dpchk = new GlideAggregate('cmdb_ci_computer');
dpchk.groupBy('serial_number');
dpchk.addHaving('COUNT', '>', 1);
dpchk.query();
while(dpchk.next())
{
// gs.print(dpchk.serial_number);
}
return dupRecords;
}

Report:

find_real_file.png

 

Priyanka Pednek
Kilo Guru

Hi Deepthi,

 

A script is the way to go on this one.   I rigged this duplicate finder up.   Feel free to alter it for your needs.   I ran this as a background script in my instance with out issue (or results, we don't have any dupes!).

 

 //highlight duplicate users
var arr = [];
var users = new GlideRecord('sys_user');
      users.addActiveQuery();
      users.query();
      while(users.next()) {
            arr.push(users.name.toString());
      }
     
//sort array by name, duplicate name are placed next to each other in the array
      arr.sort(function(a,b) {  
          return a<b ? 1 : a>b ? 1 : 0; });
                                                       
//remove duplicates:
    for(var j=0, len = arr.length, count=0, dupes=[]; j != len; j+=1) {
            if ( arr[j] == arr[j+1] ) {//if the name is the same as the name in the next arr index
                  ++count;
                  dupes.push(arr[j]);
            }
    }
//Output:
    gs.log("There are " + count + " duplicate users detected.\nUsers found: ")
    for (var k=0; k!=dupes.length;k+=1) {
    gs.log(dupes[k]);
    }

 

You can find below link that may help you to understand .

https://community.servicenow.com/community?id=community_question&sys_id=94ce0b61dbdcdbc01dcaf3231f96...

Mark Correct if this solves your issue and also mark Helpful if you find my response worthy based on the impact.
Thanks Deepthi