How to get duplicate user records using script
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-12-2024 06:33 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-12-2024 09:49 AM
Hello @MK21 ,
Comment this line and check once whether you're able to get expected results.
duplicateUserGR.addEncodedQuery('name=javascript:gs.userName();');
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-12-2024 09:59 AM
yes it works is it any way to download duplicate user details in excel using this script
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-12-2024 12:33 PM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-12-2024 09:36 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-12-2024 06:56 AM
Hi @MK21
I tried this on user table:
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]
****************************************************************************************************************