Create a report to display duplicate users

mattyb
Kilo Explorer

Hi all,

 

Im trying to generate a report to Identify users that have duplicate entries. For us, these are most often caused by the user emailing before service now gets their information.

 

So far, i have gotten a decent list of users but i am wondering how to compare them to the current users in the system. As of right now, i use location and employee type as empty which yields a majority of them but, i would like to compare them against the rest of the user base such as a same as which checks a last name on the list with other names that didnt meet my criteria.

 

If this doesnt make sense, please let me know.

 

Regards

 

MattyB

1 ACCEPTED SOLUTION

Hi mattyb,



I logged into the demo instance you linked.   I suspect the duplicate finder script didn't work for you because the field name in my instance is different from the field name in the Demo instance.



The code was looking for this field (in bold):


while(users.next()) {
arr.push(users.name.toString());
}


In the demo instance, name is undefined.   The field is actually user_name.   I attached a screenshot highlighting the demo field name:SN-Demo-user_name.JPG



So to make it work you'd have to adjust the field in the script:


while(users.next()) {
arr.push(users.user_name.toString());
}



Of course to make it show results you will have to have some existing duplicate user_name's in the instance.



The whole thing:


//highlight duplicate users


var arr = [];


var users = new GlideRecord('sys_user');


users.addActiveQuery();
users.query();
while(users.next()) {
arr.push(users.user_name.toString());
}


//sort:


arr.sort(function(a, b) {
if (a<b) { return 1 }
if (a>b) { return -1 }
return 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]);


    }    





I went to the Demo instance and tried creating some duplicates but I ran into a DB error:


SN-Demo-Key_Violation.JPG



So instead of creating user's with duplicate user_name's, we can just point the script at first_name instead to show first name duplicates.   The concept is the same.



Here I created 2 users with the first_name of 'TEST':


SN-Demo-TestUsers.JPG



Next I edited the script to push the first_name field (instead of user_name):


while(users.next()) {
arr.push(users.first_name.toString());
}


Finally I ran the background script (TEST is in the list):


SN-Demo-First_Name.JPG


View solution in original post

8 REPLIES 8

mattyb
Kilo Explorer

Would it be easier to write a script instead of generating a report?


justin_drysdale
Mega Guru

Hi mattyb,



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


Thanks justin, i ran this in the demo instance and it returns nothing even though i have created multiple duplicate users. their login id's are the only thing that have to be unique.I have it set to run as a back end script. Any thoughts?


Yep, I goofed up the sort function, please replace:



arr.sort(function(a,b) {

          return a<b ? 1 : a>b ? 1 : 0; });




With:



arr.sort(function(a, b) {

          if (a<b) { return 1 }


          if (a>b) { return -1 }


          return 0;


          });



I created 2 identical test users in my instance and the script picked it up this time.