Email notification is not working using email script
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-09-2023 12:21 AM
I am trying to add group on email notification using email script. but it is not sending notification to group members.
Please refer the code and suggest me :
(function runMailScript(current, template, email, email_action, event) {
var ritm = new GlideRecord('sc_req_item');
ritm.addQuery("request", current.request.sys_id);
ritm.query();
if (ritm.next()) {
var loc = current.u_item_for.location.name; //user location
gs.log("xyz" + loc);
}
var emails = [];
var userName = [];
var gr = new GlideRecord('sys_user_grmember');
if (loc.includes('Operations') && loc.slice(-2) == 'MA') { //if users location contain Operations and Ma notification goes to ServiceNow.Application.Approver.Metrc.MAGrowCompliance group.
gr.addQuery("group.name", "ServiceNow.Application.Approver.Metrc.MAGrowCompliance");
gr.query();
while (gr.next()) {
emails.push(gr.user.email.toString());
userName.push(gr.user.toString());
email.addAddress("cc", gr.user.email.toString(), gr.user.getDisplayValue());
gs.info("Tanuj" + emails.toString());
gs.info("Mahesh" + userName.toString());
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-09-2023 07:53 AM
Hello @mahesh105 ,
Below thread might be helpful for you:
mail_script why does it not populate the cc field?
Please mark this comment as correct/helpful if it helps you.
Regards,
Shubham

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-09-2023 08:17 AM - edited 02-09-2023 08:18 AM
Hi, a couple things to look at that may help:
- confirm that your dot-walking is actually returning a value
- add a log statement after the email.addAdress line: gs.info("CC: " + gr.user.email.toString() + ", " + gr.user.getDisplayValue()); and see if the values are what you expect
- make sure that your mail_script tag is in the body of the email
-Art
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-13-2023 02:19 AM
Hi @art_jones : It is printing email address in logs bit not sending any notification
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-09-2023 09:57 AM - edited 02-09-2023 10:00 AM
Try Below
(function runMailScript(current, template, email, email_action, event) {
var ritm = new GlideRecord('sc_req_item');
ritm.addQuery("request", current.request.sys_id);
ritm.query();
if (ritm.next()) {
var loc = current.u_item_for.location.name; //user location
gs.log("xyz" + loc);
var emails = [];
var userName = [];
var gr = new GlideRecord('sys_user_grmember');
if (loc.includes('Operations') && loc.slice(-2) == 'MA') { //if users location contain Operations and Ma notification goes to ServiceNow.Application.Approver.Metrc.MAGrowCompliance group.
gr.addQuery("group.name", "ServiceNow.Application.Approver.Metrc.MAGrowCompliance");
gr.query();
while (gr.next()) {
emails.push(gr.user.email.toString());
userName.push(gr.user.toString());
email.addAddress("cc", gr.user.email.toString(), gr.user.getDisplayValue());
gs.info("Tanuj" + emails.toString());
gs.info("Mahesh" + userName.toString());
}
}
}
})(current, template, email, email_action, event);