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

Aniket Chavan
Tera Sage
Tera Sage

Hello @MK21 ,

 

Please try with the script below and let me know how it works for you.

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

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

// Finding users based on last login in the last 3 months
var last3MonthsAgo = new GlideDateTime();
last3MonthsAgo.addMonthsUTC(-3);

var last3MonthsGR = new GlideRecord('sys_user');
last3MonthsGR.addQuery('last_login', '>=', last3MonthsAgo);
last3MonthsGR.query();

while (last3MonthsGR.next()) {
    gs.info('User ' + last3MonthsGR.name + ' logged in within the last 3 months.');
}

// Finding users based on last login in the last 6 months
var last6MonthsAgo = new GlideDateTime();
last6MonthsAgo.addMonthsUTC(-6);

var last6MonthsGR = new GlideRecord('sys_user');
last6MonthsGR.addQuery('last_login', '>=', last6MonthsAgo);
last6MonthsGR.query();

while (last6MonthsGR.next()) {
    gs.info('User ' + last6MonthsGR.name + ' logged in within the last 6 months.');
}

 

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 Hi 

 

it works but it comes like this 

User Jet logged in within the last 6 months.

 i need duplicate users list and who are not before 6 months 

Hello @MK21 ,

Please try with the below modified script once and let me know how it works for you.

// 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();

while (duplicateUserGR.next()) {
    gs.info('Duplicate user record found for ' + duplicateUserGR.name);
    
    // 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.');
    }
}

 

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

 

No Luck

 

getting like this

 

MK21_0-1705080386418.png