Email body issue it contains duplicate details
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-18-2024 05:00 AM
Hi, I am trying to delete the itil role from users who are no logged in system last 60 days. I am able to delete the itil role but I am having issue while sending email with itil users removal list in email body. The email boday contains duplicate users list.
Code:
It should be like below.
What is the issue in my code? kindly help on it.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-18-2024 05:16 AM
Hello Sarah,
This could happen because the user might have multiple roles or group memberships, and as a result, the user gets added to the removedUsers
list multiple times.
so please ensure that each user is only added once to the removedUsers list.
One way to do this is by checking if the user already exists in the list before adding them.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-18-2024 06:12 AM
Hi Prasad, Thank you for your response. How can we do this? please help me with some code, what code should include it. I think your proposed solution will help us.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-18-2024 05:35 AM
Hi @Sarah Bouil ,
Please replace your GlideEmailOutbound code with the following code. If a user has the itil role inherited multiple times (2 or 3 times), they will only receive the email notification once.
var daysAgo = gs.daysAgoStart(60); // Get date for 60 days ago
var emailBody = "The following users had their 'itil' role removed because they have not logged in for 60 days:\n\n";
// Query sys_user for users who haven't logged in for 60 days and have the 'itil' role
var userGr = new GlideRecord('sys_user');
userGr.addEncodedQuery('last_login_time<=' + daysAgo + '^roles=itil'); // Users who haven't logged in for 60 days and have 'itil' role
userGr.addActiveQuery(); // Only active users
userGr.query();
while (userGr.next()) {
var email = new GlideEmailOutbound();
email.setSubject('ITIL access removed users list');
email.addRecipient(userGr.email);
email.addRecipient(userGr.manager.email);
email.setBody(emailBody);
email.save();
email.send();
gs.log('Notification email sent to user');
}
Please mark my answer as solution accepted and indicate whether it was helpful in resolving your queries.
Regards
Moin
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-18-2024 06:08 AM
Hi Moin, thank you for you reply. Your solution is not helping.
My requirements are: if user not logged into the system in 60 days.
1. itil role should be remove from user if the itil role is explicitly added - working fine
2. itil and parent role should be remove from user if the itil role is inherited from any other role(if itil is inherited from knowledge_manager role, so both itil and knowledge_manager should be removed from user) - working fine
3. Group should be remove from user if the itil role is inherited from group - working fine
and last one
4. if I am removing itil role from users so the email should be sent to ex: test1@example.com or test2@example.com and email body should contains with user details(like Name, User ID, Email) - it is working but duplicate user details
With my code, first 3 requirements are working fine
I have an issue with 4th requirement, kindly suggest.