Notification issue
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-15-2024 09:11 AM
Hi removing itil role from users if they not logged into the system in 60 days. I am able to remove itil role from users and able to send an email as well but 2 emails are sending, it should be sent only one email with list of removal itil users.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-15-2024 03:55 PM
Hello
It's triggering twice because it's in the while loop of user GlideRecord. Could you try to push the users in the array for once and then GlideRecord the email table?
Also, why are you using GlideEmailOutbound API? Instead, why aren't you using an event to trigger a notification?
See your code below for reference -
var ITIL_ROLE_ID = '282bf1fac6112285017366cb5f867469';
function getContainingRoles(roleSysId, rolesToRemove) {
var grRoleContains = new GlideRecord('sys_user_role_contains');
grRoleContains.addQuery('contains', roleSysId);
grRoleContains.query();
while (grRoleContains.next()) {
var containingRoleId = grRoleContains.role.toString();
if (!rolesToRemove.includes(containingRoleId)) {
rolesToRemove.push(containingRoleId);
getContainingRoles(containingRoleId, rolesToRemove);
}
}
}
var rolesToRemove = [ITIL_ROLE_ID];
getContainingRoles(ITIL_ROLE_ID, rolesToRemove);
var removedUsers = [];
var userGr = new GlideRecord('sys_user');
userGr.addEncodedQuery('active=true^last_login<=javascript:gs.beginningOfLast60Days()^roles=itil^web_service_access_only=false^ORinternal_integration_user=false^user_nameNOT LIKEadmin^user_nameNOT LIKEsvc^nameNOT LIKEsvc');
userGr.query();
gs.log('User count: ' + userGr.getRowCount());
while (userGr.next()) {
gs.print('Processing User: ' + userGr.getDisplayValue('user_name'));
var groupList = [];
var userRoleGr = new GlideRecord('sys_user_has_role');
userRoleGr.addQuery('user', userGr.sys_id);
userRoleGr.addQuery('role', 'IN', rolesToRemove.join(','));
userRoleGr.query();
while (userRoleGr.next()) {
gs.log('Removing explicit ITIL role for user: ' + userGr.user_name);
//userRoleGr.deleteRecord();
removedUsers.push({
name: userGr.getDisplayValue('name'),
userId: userGr.getDisplayValue('user_name'),
email: userGr.getDisplayValue('email')
});
}
gs.print('Test01-removedUsers: ' + removedUsers.length);
var groupMemberGr = new GlideRecord("sys_user_grmember");
groupMemberGr.addQuery("user", userGr.sys_id);
groupMemberGr.query();
while (groupMemberGr.next()) {
var groupRoleGr = new GlideRecord("sys_group_has_role");
groupRoleGr.addQuery("group", groupMemberGr.group);
groupRoleGr.addQuery("role", 'IN', rolesToRemove.join(','));
groupRoleGr.query();
if (groupRoleGr.next()) {
gs.log('User ' + groupMemberGr.getDisplayValue('user') + ' removed from group ' + groupMemberGr.group.name + ' due to License Optimization');
groupList.push(groupMemberGr.group.name.toString());
//groupMemberGr.deleteRecord();
}
}
gs.print('Test01-groupList: ' + groupList.length);
}
if (removedUsers.length > 0) {
var emailBody = "The ITIL role has been removed from the following users due to inactivity:\n\n";
for (var i = 0; i < removedUsers.length; i++) {
var user = removedUsers[i];
emailBody += "Name: " + user.name + ", User ID: " + user.userId + ", Email: " + user.email + "\n";
}
var email = new GlideEmailOutbound();
email.setSubject('ITIL access removed users list');
email.addRecipient('survey.user@email.com');
email.addRecipient('test1@example.com');
email.setBody(emailBody);
email.save();
email.send();
gs.log('Notification email sent to user');
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-16-2024 04:20 AM
Hi Mukesh,
Thank you! Yes, it's sending one email only.
But the email body contains the user details multiple times, highlighted below.
It should be like below.