How to get one email notification if user is inactive

prasad39
Tera Contributor

Hi Everyone, 

 

I have a requirement 

Need To Identify When User Marked As Inactive Is Also Assigned As A Group Manager.

 

I have created one BR on Sys_user table triggering Notification currently.

 var gr = new GlideRecord('sys_user_group');
    gr.addEncodedQuery('active=true^manager.active=false');
    gr.query();
    while (gr.next()) {
        gs.eventQueue("user.inactive.group.manager", gr);
    }

 

Here the issue is If one is user is manager for Multiple groups it triggering notification for each group one notification.

I need to send only one notification If the user is manager for multiple groups and also need to Update all the groups name in notification body.

 

Thanks,

Prasad

8 REPLIES 8

You need to change your glide record in the mail script to query the manager so that it returns all groups for that manager, and change you parm1 to gr.manager. Your script will search for groups where the group name is equal to event.parm1 which is likely a single group.

Jack Littlewort
Giga Guru

hi @prasad39,

 

In your BR you would simply need to check for if the user is a manager of any group. (as below)

var gr = new GlideRecord('sys_user_group');
gr.addEncodedQuery('active=true^manager=' current.sys_id);
gr.setLimit(1);
gr.query();
if (gr.next()) {
 gs.eventQueue("user.inactive.group.manager", gr);
}

Then in your email notification that is triggered from this event you can add a mail script which can perform a glide record and list all of the groups that use is a member of. (Scripting for email notifications (servicenow.com)) 

Amit Gujarathi
Giga Sage
Giga Sage

HI @prasad39 ,
i trust you are doing great.
Please find the updated script for the same

(function executeRule(current, previous /*null when async*/) {

    // Object to hold managers and their groups
    var managers = {};

    // Query to find active groups with inactive managers
    var gr = new GlideRecord('sys_user_group');
    gr.addEncodedQuery('active=true^manager.active=false');
    gr.query();
    while (gr.next()) {
        var managerId = gr.manager.sys_id.toString();
        var groupName = gr.name.toString();

        // Check if this manager is already in the object
        if (!managers[managerId]) {
            managers[managerId] = {
                name: gr.manager.getDisplayValue(),
                groups: []
            };
        }

        // Add group to the manager's list
        managers[managerId].groups.push(groupName);
    }

    // Send notification for each manager with all their groups
    for (var id in managers) {
        var managerInfo = managers[id];
        var groupList = managerInfo.groups.join(', ');

        // Set up the event with the necessary information
        var eventParms = {
            'manager_name': managerInfo.name,
            'group_list': groupList
        };

        // Trigger the event once per manager
        gs.eventQueue("user.inactive.group.manager", null, id, JSON.stringify(eventParms));
    }

})(current, previous);

Was this answer helpful?


Please consider marking it correct or helpful.


Your feedback helps us improve!


Thank you!


Regards,


Amit Gujrathi



Hi @Amit Gujarathi , 

Thanks for response.

I tried this script is not working as expected.

In my case, If P is the user inactive the same user is manager for any groups, Then i need to send notification to ABC group. 

 

In notification body 

 

Group Names: X, Y, Z.

Manager Name:  P

 

Here, P is manager for x,y,z groups so i need to print those group names and notification should only trigger once to ABC group for one user.