How to get duplicate user records using script

MK21
Tera Contributor

Hi All,

 

I have a requirement , to find the duplicate user records based on the name and to find the users based on the last login in 3 months and 6 months

 

looking your help 

12 REPLIES 12

Hello @MK21 ,

Comment this line and check once whether you're able to get expected results.
duplicateUserGR.addEncodedQuery('name=javascript:gs.userName();');

@Aniket Chavan 

yes it works is it any way to download duplicate user details in excel using this script

MK21_0-1705082286610.png

 

Hello @MK21 ,

Yes we can do that, please find the modified code below.

// Finding duplicate users based on name
var duplicateUserGR = new GlideRecord('sys_user');
//duplicateUserGR.addEncodedQuery('name=javascript:gs.userName();'); // Adjust the condition as needed
duplicateUserGR.groupBy('name');
duplicateUserGR.addHaving('COUNT', '>', 1);
duplicateUserGR.query();

var csvData = "UserID,Name,Company\n"; // CSV Header

while (duplicateUserGR.next()) {
    gs.info('Duplicate user record found for ' + duplicateUserGR.name);

    // Add user details to CSV data
    csvData += duplicateUserGR.sys_id + ',' + duplicateUserGR.name + ',' + duplicateUserGR.company + '\n';

    // Now, let's check the last login for each duplicate user
    var lastLogin = new GlideDateTime(duplicateUserGR.last_login);
    var last6MonthsAgo = new GlideDateTime();
    last6MonthsAgo.addMonthsUTC(-6);

    if (lastLogin.compareTo(last6MonthsAgo) >= 0) {
        gs.info('User ' + duplicateUserGR.name + ' logged in within the last 6 months.');
    } else {
        gs.info('User ' + duplicateUserGR.name + ' did not log in within the last 6 months.');
    }
}

// Save CSV file as an attachment
var grAttachment = new GlideSysAttachment();
grAttachment.write(duplicateUserGR, "DuplicateUserDetails.csv", 'application/csv', csvData);

 

Let me know your views on this and Mark Correct if this solves your query and also mark 👍Helpful if you find my response worthy based on the impact.

 

Thanks,

Aniket

@Aniket Chavan No Luck 

 

MK21_0-1705080995494.png

 

Dr Atul G- LNG
Tera Patron
Tera Patron

Hi @MK21 

 

I tried this on user table:

LearnNGrowAtul_0-1705071289288.png

 

what is your exact requirement

 

*************************************************************************************************************
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]

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