send notification to all group managers of a user when user department changes

nabeel55
Tera Contributor

Requirement : Send Notification when user’s department is changed.

 

  1. Check User that they are in any Group Member or not.
  2. If that User is in Group Member, system remove user who change department from their previous Group Member automatically. (User should be removed from all the groups)
  3. Send notify mail to inform the previous Group Manager that the Changed department User already removed from Group, because of their department changed.
  4. If Group Manager still need to add this User back in the previous Group, can be added manually.

 

 Mail Notification: Notification can be sent when user department changed. Need ServiceNow system to send mail notify to previous Group Manager

UC1: Notify mail to the previous Group Manager

Email to (who): Notification to the previous [Group Managers] that user is removed off.

Email Body (what): User (ID and Name) department changed from Previous Department to New Department on Detected Date, and already removed from Group Member automatically.

Email Subject:  ServiceNow User Department change detected

Member name: [FirstName-LastName]

Effected Group name: Previous [GroupName]

Status: [Status]

Detected date: [xxx]

Actions: To be notified the updated group members

User is automatically removed from Group.

 How many notifications to be sent?  1 time when that user automatically removed from Group.

-----------------------------------------------------------------------------------------------------------------------------------------

so, this is my task and my approach is to 

1. create an event trigger when the user department is changed {table selected sys_user}

2. when the event is fired then the notification should run accordingly.

3. write a business rule to get all the info of the managers, which I stored in variable name managers.

4. then create another event to transfer the variable managers from business rule to email script in the recipient 

5. then wrote the email script for the custom email template i  wanted.   

-----------------------------------------------------------------------------------------------------------------------------------------

but it's not working!!  below are the business rule i used and also the email script i used 

Business Rule Script:

// Event Registry for User Department Change
(function executeRule(current, previous) {
    gs.eventQueue('UserDepartmentChange', current, previous);
})();

// Function to get the managers from the previous groups of the user
function getManagersFromPreviousGroups(userId) {
    var managers = [];

    // Query sys_user_grmember to get the previous groups
    var grMemberGR = new GlideRecord('sys_user_grmember');
    grMemberGR.addQuery('user', userId);
    grMemberGR.query();

    while (grMemberGR.next()) {
        // For each group the user was a member of, get the manager
        var groupName = grMemberGR.getValue('group');
        var manager = getManagerFromGroup(groupName);

        // If a manager is found, add it to the list of managers
        if (manager) {
            managers.push(manager);

            // Automatically remove the user from the group member
            grMemberGR.deleteRecord(); // This deletes the sys_user_grmember record
        }
    }

    // Send notification to managers
    sendNotificationToManagers(managers);

    return managers;
}

// Function to get the manager from a specific group
function getManagerFromGroup(groupName) {
    var manager;

    // Query the sys_user_grmember table to get the manager of the specified group
    var groupManagerGR = new GlideRecord('sys_user_grmember');
    groupManagerGR.addQuery('group', groupName);
    groupManagerGR.addQuery('role', 'manager'); // Assuming 'manager' is the role of the group manager
    groupManagerGR.query();

    if (groupManagerGR.next()) {
        // Assuming 'user' is the reference field to the user in the sys_user_grmember table
        var managerId = groupManagerGR.getValue('user');

        // Get manager details from the sys_user table
        var managerGR = new GlideRecord('sys_user');
        if (managerGR.get(managerId)) {
            manager = {
                id: managerGR.getValue('sys_id'),
                name: managerGR.getValue('name'),
                email: managerGR.getValue('email')
                // Add more properties as needed
            };
            // Event trigger to send managers array from the business rule to email notification
            gs.eventQueue('importmanagers', current, current.number, managers);
        }
    }

    return manager;
}
nabeel55_0-1708507254317.png

 

-----------------------------------------------------------------------------------------------------------------------------------------

Email script i used:

 

(function runMailScript(/* GlideRecord */ current, /* TemplatePrinter */ template,
          /* Optional EmailOutbound */ email, /* Optional GlideRecord */ email_action,
          /* Optional GlideRecord */ event) {

    // Loop to add managers to "cc" list
    for (var i = 0; i < event.managers.length; i++) {
        email.addAddress("cc", event.managers[i].email, event.managers[i].name);
    }

    // Set email subject
    email.setSubject("ServiceNow User Department Change Detected");

    // Set email body
    email.setBody("User (" + current.sys_id + "-" + current.name + ") department changed from " +
                    "Previous Department to " + current.department + " on " + gs.now() +
                    ", and already removed from Group Member automatically.\n\n" +
                    "Member name: " + current.name + "\n" +
                    "Effected Group name: Previous " + current.previous_group_name + "\n" +
                    "Status: " + current.status + "\n" +
                    "Detected date: " + gs.now() + "\n" +
                    "Actions: To be notified the updated group members");

})(current, template, email, email_action, event);
 
-----------------------------------------------------------------------------------------------------------------------------------------
screenshot of event registration when the department is changed
nabeel55_1-1708507449102.png

-----------------------------------------------------------------------------------------------------------------------------------------

 

screenshot of the event registration  to get variable managers from business rule 

nabeel55_2-1708507664292.png

-----------------------------------------------------------------------------------------------------------------------------------------

screenshot of notification 

nabeel55_3-1708507748176.png

-----------------------------------------------------------------------------------------------------------------------------------------

when checking to any error in the system log then it shows Analytics framework send status not success, instead is 7

nabeel55_4-1708507875093.png

 

 

IF ANYONE HAS MORE EASIER WAY TO PERFORM THE TASKS PLEASE LET ME KNOW!!

2 REPLIES 2

Mark Manders
Mega Patron

I think you need to start by going back to the person giving you this requirement. Why are you removing a person from all groups on department change and asking the manager of the group if the person is correctly removed? If the answer is 'yes', this person is unable to do his job until the group manager puts him back in. Shouldn't it be the other way around: "Dear group manager, John has moved from department X to department Y. Please validate if he needs to remain member of your group for doing his Job."
You could even just create approval records for this: approved = stay, rejected is auto-remove.


Please mark any helpful or correct solutions as such. That helps others find their solutions.
Mark

the above requirement is correct the managers can add the use again if needed