Getting Manager and User from user list variable

Cirrus
Kilo Sage

Hi,

In a workflow, we collect a list of users in a list collector variable, and in the workflow we have a notification step, such that if user A and User B are selected, the notification step would send two separate emails, one to Manager A, one to Manager B, with body text Your User A has been selected or Your User B has been selected. The below script is getting the managers ok but sending them the same email. How can I amend it to send to each manager in turn, and also get the user value (u) and pass that as a value in the body text 

 

 

answer = [];

var emailSysId = [];
var user = current.variables.selected_name.toString().split(',');
for (var i = 0; i < user.length; i++) {
var u = new GlideRecord('sys_user');

if (u.get(user[i])) {


answer.push(u.manager.toString());
}

}

 

 

2 REPLIES 2

Ratnakar7
Mega Sage
Mega Sage

Hi @Cirrus ,

To send separate emails to each manager with the corresponding user's name in the body text, you can modify the script as follows:

 

answer = [];

var emailSysId = [];
var user = current.variables.selected_name.toString().split(',');
for (var i = 0; i < user.length; i++) {
  var u = new GlideRecord('sys_user');

  if (u.get(user[i])) {
    // Get the manager's email address
    var managerEmail = u.manager.email.toString();
    // Get the user's name
    var userName = u.name.toString();

    // Send the email to the manager
    gs.eventQueue('email.sent', u, managerEmail, 'Your User ' + userName + ' has been selected');
  }
}

 

In the above script, we retrieve the manager's email address using u.manager.email.toString() and the user's name using u.name.toString(). Then, we use the gs.eventQueue method to send separate emails to each manager with the corresponding user's name in the body text.

Make sure you have the appropriate notification setup to handle the email.sent event and trigger the email to the managers.

 

Thanks,

Ratnakar

Ratnakar,

Thanks for your help on this. I created an email.sent event on the sc_req_item table in the registry, and a notification (also on sc_req_item table), ticked event parameter 1 contains recipient, but no recipients are generated. Not sure if this is the right thing to do as you appear to define the email body in the script.