Email body issue it contains duplicate details

Sarah Bouil
Tera Expert

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:

 

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&colon;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');
}
 
Email body: email body contains the user details multiple times, highlighted below.
SarahBouil_0-1729252452395.png

 

It should be like below.

 

SarahBouil_1-1729252825179.png

What is the issue in my code? kindly help on it.

6 REPLIES 6

Prasad Dhumal
Mega Sage
Mega Sage

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.

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.

Moin Kazi
Kilo Sage
Kilo Sage

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

 

 

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.