How to send a single email notification to a manager for multiple users meeting specific conditions

AbdulrehmanT
Kilo Guru

Description: I’m trying to implement a solution in ServiceNow where: 1. Conditions: Users must have city = Lahore and role = bsm_legacy. 2. Notification: If a manager has multiple users meeting these conditions, I want to send a single email notification to the manager. The email should include a list of all users who meet the conditions.

What I’ve Tried: 1. I created a Scheduled Job to group users by their manager and compile a list of users who meet the conditions. 2. I used the Notification framework to send an email to the manager with the list of users. 3. Here’s the script I used in the Scheduled Job:

(function executeJob() { // Query all users who meet the conditions var userGr = new GlideRecord('sys_user'); userGr.addQuery('city', 'Lahore'); userGr.addQuery('role', 'bsm_legacy'); userGr.query(); // Create a map to group users by manager var managerMap = {}; while (userGr.next()) { var manager = userGr.manager.toString(); if (!managerMap[manager]) { managerMap[manager] = []; } managerMap[manager].push(userGr.getDisplayValue('name')); } // Iterate through the manager map and send notifications for (var managerSysId in managerMap) { var userList = managerMap[managerSysId].join("\n"); // Get the manager's record var managerGr = new GlideRecord('sys_user'); if (managerGr.get(managerSysId)) { // Log the manager and user list gs.info("Sending email to manager: " + managerGr.email + ", User List: " + userList); // Send the email notification using the Notification framework try { var notification = new GlideNotification(); notification.setType('email'); notification.setRecipient(managerGr.email); notification.setSubject('Notification: Users Meeting Conditions'); notification.setBody("The following users meet the specified conditions:\n\n" + userList); notification.send(); gs.info("Email sent successfully to: " + managerGr.email); } catch (e) { gs.error("Failed to send email to: " + managerGr.email + ", Error: " + e.message); } } } })();

Issue: The script runs without errors, but no emails are being sent to the manager’s Gmail account. I’ve verified the following: - The Outbound Email Configuration in ServiceNow is correct. - The Gmail settings allow less secure apps, and I’ve also tried using an app password. - The logs show that the script is executing, but there are no errors related to email sending.

Questions: 1. Is there anything wrong with the script or the approach I’m using? 2. How can I debug why emails are not being sent to external email addresses like Gmail? 3. Are there any additional configurations or permissions required in ServiceNow to send emails to external addresses?

1 ACCEPTED SOLUTION

GopikaP
Mega Sage

Hi @AbdulrehmanT , instead of GlideNotification API - use - GlideEmailOutbound (Refer -GlideEmailOutbound()-Send Notifications using Scri... - ServiceNow Community)

var userGr = new GlideRecord('sys_user');
userGr.addQuery('city', 'Lahore');
userGr.query(); // Create a map to group users by manager 
var managerMap = {};
while (userGr.next()) {
    var manager = userGr.manager.name.toString();
    if (!managerMap[manager]) {
        managerMap[manager] = [];
    }
    managerMap[manager].push(userGr.getDisplayValue('name'));
}
for (var managerSysId in managerMap) {
    var userList = managerMap[managerSysId].join("\n"); // Get the manager's record 
    var managerGr = new GlideRecord('sys_user');
    if (managerGr.get(managerSysId)) {
        // Log the manager and user list 
        gs.info("Sending email to manager: " + managerGr.email + ", User List: " + userList); // Send the email notification using the Notification framework 
        try {
            var notification = new GlideEmailOutbound();
			notification.setSubject('Notification: Users Meeting Conditions');
			notification.addRecipient(managerGr.email);
            notification.setBody("The following users meet the specified conditions:\n\n" + userList);
            notification.save();
            gs.info("Email sent successfully to: " + managerGr.email);
        } catch (e) {
            gs.error("Failed to send email to: " + managerGr.email + ", Error: " + e.message);
        }
    }
}

 

View solution in original post

2 REPLIES 2

GopikaP
Mega Sage

Hi @AbdulrehmanT , instead of GlideNotification API - use - GlideEmailOutbound (Refer -GlideEmailOutbound()-Send Notifications using Scri... - ServiceNow Community)

var userGr = new GlideRecord('sys_user');
userGr.addQuery('city', 'Lahore');
userGr.query(); // Create a map to group users by manager 
var managerMap = {};
while (userGr.next()) {
    var manager = userGr.manager.name.toString();
    if (!managerMap[manager]) {
        managerMap[manager] = [];
    }
    managerMap[manager].push(userGr.getDisplayValue('name'));
}
for (var managerSysId in managerMap) {
    var userList = managerMap[managerSysId].join("\n"); // Get the manager's record 
    var managerGr = new GlideRecord('sys_user');
    if (managerGr.get(managerSysId)) {
        // Log the manager and user list 
        gs.info("Sending email to manager: " + managerGr.email + ", User List: " + userList); // Send the email notification using the Notification framework 
        try {
            var notification = new GlideEmailOutbound();
			notification.setSubject('Notification: Users Meeting Conditions');
			notification.addRecipient(managerGr.email);
            notification.setBody("The following users meet the specified conditions:\n\n" + userList);
            notification.save();
            gs.info("Email sent successfully to: " + managerGr.email);
        } catch (e) {
            gs.error("Failed to send email to: " + managerGr.email + ", Error: " + e.message);
        }
    }
}

 

Ankur Bawiskar
Tera Patron
Tera Patron

@AbdulrehmanT 

if you are seeing email in email logs and actual email is not received then please check with your mailbox team why it's not sent to those users

If the email itself is not coming in email logs then you can debug it within ServiceNow instance

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader