Email Script - Add users to CC field

Edwin Fuller
Tera Guru

I have a notification that is sent out each time an event/outage is created. Each event has one or more "Affected CI" that the event could pertain to. For each CI we have a support group (support_group) associated to it.

For each affected CI selected,I need to include the support group in the CC field of the notification.

FYI, Affected CI(u_affected_ci) is an list type field which references the "cmdb_ci" table. The event table is called "cmdb_ci_outage".

I have the below script and it is NOT adding the users to the CC field. I think I may have configure lines 26-28 incorrectly. Could someone look at my code and advice where changes should be?

 

Email Script

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

// Add your code here
if (!current.u_affected_ci.nil()) {
//get all affected CI's as string separated by comma
var commaDelimetedAffectedCI = current.u_affected_ci.toString();
var affectedCiArray = commaDelimetedAffectedCI.split(',');
var userList = [];

//gs.addInfoMessage(commaDelimetedAffectedCI);
for(var i = 0; i < affectedCiArray.length; i++){

var apps = new GlideRecord("cmdb_ci");
apps.addQuery("sys_id", affectedCiArray[i]);
apps.query();
while (apps.next()) {
var supportGroup = apps.support_group;
//gs.addInfoMessage(supportGroup);

var mem = new GlideRecord("sys_user_grmember");
mem.addQuery('group', supportGroup);
mem.query();
while (mem.next()) {
userList.push(mem.user.email.toString());
//gs.addInfoMessage(userList);
email.addAddress("cc", userList);
}
}

}
}

})(current, template, email, email_action, event);

1 ACCEPTED SOLUTION

user

 

while (mem.next()) { 
//userList.push(mem.user.email.toString());
//gs.addInfoMessage(userList);
email.addAddress("cc", mem.user.email,mem.user.getDisplayValue());


}

View solution in original post

4 REPLIES 4

sachin_namjoshi
Kilo Patron
Kilo Patron

hi,

 

you need to add display name as third paramter like below

 

email.addAddress("cc","john.secret@example.com","John Roberts");

Regards,
Sachin

user

 

while (mem.next()) { 
//userList.push(mem.user.email.toString());
//gs.addInfoMessage(userList);
email.addAddress("cc", mem.user.email,mem.user.getDisplayValue());


}

Shishir Srivast
Mega Sage

Can we try like, I don't think you need to push the users into an array.

if (!current.u_affected_ci.nil()) {
//get all affected CI's as string separated by comma
var commaDelimetedAffectedCI = current.u_affected_ci.toString();
var affectedCiArray = commaDelimetedAffectedCI.split(',');
//gs.addInfoMessage(commaDelimetedAffectedCI);
for(var i = 0; i < affectedCiArray.length; i++){

var apps = new GlideRecord("cmdb_ci");
apps.addQuery("sys_id", affectedCiArray[i]);
apps.query();
while (apps.next()) {
var supportGroup = apps.support_group;
//gs.addInfoMessage(supportGroup);

var mem = new GlideRecord("sys_user_grmember");
mem.addQuery('group', supportGroup);
mem.query();
while (mem.next())
email.addAddress("cc", mem.user.email.totring(), mem.user.name.toString());
}
}
}

Edwin Fuller
Tera Guru

Thank you both for your response. I'm going to mark Kumar response as correct since it worked for me

 

Thanks 🙂