Group by users with managers

Devika3
Tera Guru

Hi All,

We have to implement a notification to the managers when the account of the reportees are going to expire,

 

ie, The contractor accounts should be disabled after 90 days of creation. Before user expiration we need to send a notification to the managers with the details  of user accounts that are going to expire.

 

Here the manager should get a single notification with the details of his direct reportees. 

So in the scheduled job i need to find the users grouping by their manager, but i am not able to achieve that, Can anyone please guide me on this.

Regards,

Devika.

2 REPLIES 2

chetanb
Tera Guru

Did you make any try at your end. Any script which is failing to get the desired output etc.

Mark Manders
Mega Patron

You could use something like this as a base:

 

// Define the current date and the date 90 days from now
var currentDate = new GlideDateTime();
var ninetyDaysLater = new GlideDateTime();
ninetyDaysLater.addDays(90);

// Query the User table for users with contracts expiring within the next 90 days
var userGr = new GlideRecord('sys_user');
userGr.addQuery('contracted', 'true'); // Assuming 'contracted' is the field indicating the user is contracted
userGr.addQuery('expire', '>=', currentDate); // Assuming 'expire' is the contract expiration date field
userGr.addQuery('expire', '<=', ninetyDaysLater);
userGr.query();

var managers = {}; // Object to hold manager-wise user lists

while (userGr.next()) {
  var managerId = userGr.manager; // Assuming 'manager' is the field for the user's manager
  if (!managers[managerId]) {
    managers[managerId] = [];
  }
  // Add the user and their expiration date to the manager's list
  managers[managerId].push({
    name: userGr.name.toString(),
    expire: userGr.expire.toString()
  });
}

// Send an email to each manager with their users' contract expiry details
for (var mgr in managers) {
  var mgrRecord = new GlideRecord('sys_user');
  if (mgrRecord.get(mgr)) {
    var email = mgrRecord.email.toString(); // Email of the manager
    var subject = "Contract Expiry Notification";
    var message = "The following contracted users have their contracts expiring within 90 days:\n\n";

    managers[mgr].forEach(function(user) {
      message += "User: " + user.name + ", Expiry Date: " + user.expire + "\n";
    });

    // Send the email
    gs.sendEmail({
      to: email,
      subject: subject,
      body: message
    });
  }
}

 

Or trigger a notification with an event to apply email scripts/templates.


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