Notification email script not working - could really use some help!!

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-03-2022 04:58 PM
Hello SN Developer Community!!
I have a requirement to send all external notifications to users on the email "bcc" field, rather than the "to" or "cc" field. The notifications are running against a custom table (u_operational_mechanical_incident) where there are custom incident forms and based on one field on each form called "Customers" (u_users) - please see screenshot below.
I have 3 notifications for Urgent / High incidents that have the mail script (copied below) added to the body of the email(s) - but the notifications are still listing the customers out on the "To" field, rather than the "Bcc" field. Can anyone please tell me what I'm doing wrong here?
(function runMailScript( /*GlideRecord */ current, /* TemplatePrinter */ template,
/* Optional EmailOutbound */
email, /*Optional GlideRecord */ email_action,
/* Optional GlideRecord */
event) {
var user = new GlideRecord("u_operational_mechanical_incident");
user.addQuery("u_users", current.getUniqueValue());
user.addQuery("email", "!=", "");
user.query();
while (user.next()) {
email.addAddress("bcc", user.getValue('email'), user.getValue('name'));
}
})(current, template, email, email_action, event);
- Labels:
-
Scripting and Coding

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-04-2022 07:50 AM
Hello Shloke!
Thank you so much for your reply. I checked the sys property you referenced and the value is empty. I also ran through the list of articles on the Now Knowledge base seeking an answer but could not find one there either (the list here: https://support.servicenow.com/kb?id=kb_article_view&sysparm_article=KB0957766).
I think the issue has to be somewhere in my script, but I'm not seeing it. Does anything there stand out to you? Please advise.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-04-2022 09:57 AM
Okay saw your script above.
Since your Notification on the same table where Customer field is present so you do not need to Glide Record it. You can directly reference the field and trigger Notification using the script shown below:
Please follow the steps below to achieve your requirement:
1) Create a Notification Script with below:
Script:
(function runMailScript( /* GlideRecord */ current, /* TemplatePrinter */ template,
/* Optional EmailOutbound */
email, /* Optional GlideRecord */ email_action,
/* Optional GlideRecord */
event) {
// Add your code here
if (!current.u_users.nil()) { // Replace "u_users" with correct Field Name
var watcherIds = current.u_users.split(",");// Replace "u_users" with correct Field Name
//get user records
var user = new GlideRecord("sys_user");
user.addQuery("sys_id", watcherIds);
//email
user.addQuery("email", "!=", "");
user.query();
while (user.next()) {
//add to bcc list
email.addAddress("bcc", user.email, user.getDisplayValue());
}
}
})(current, template, email, email_action, event);
Now call this Email Script in your Notification using the Syntax below which need to be written in Body of the Notification as shown below:
${mail_script:trigger_bcc_notif} // Replace "trigger_bcc_notif" with your Mail Script Name
Output:
Hope this helps. Please mark the answer as correct/helpful based on impact.
Regards,
Shloke
Regards,
Shloke

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-04-2022 11:08 AM
THANK YOU sooo very much Shloke04!!!
I'm MUCH closer than I was before, as I now see the intended recipient in the bcc field. But now, I am also seeing the notifications go to unintended users and on the "To" field - and I am not sure why since there are no users added to the watch list and I do not have the option checked to notify the event creator. Do you know what might be causing this issue? Please see the screenshot below. The email(s) should not go out to "tonyavaldez@me.com" since she is not listed in the Customers field, yet it is - and on the "To" line. The email script that I am working on to put the recipients on the bcc line is the only one referenced in the notification. Please advise (and THANK YOU again for your help with this!!!)

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-04-2022 03:20 PM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-04-2022 09:20 PM
This is not possible, there should be some other logic which might be causing this to happen in your Instance.
I have just tested for my Incident form by adding "Abel Tuter" on the Watchlist. I also have Send to Event Creator checked as True and Watch List selected in my Notification Recipient List and only Abel Tuter gets the email.
For example, refer to the Incident record first:
My Notification Below:
Output:
Yes, BCC one will be there in TO Recipient field which is OOB behavior but it will not add a Random user
Can you share the Notification screenshot of what you have configured and also the Record screenshot for which you are testing to assist you further.
Hope this helps. Please mark the answer as correct/helpful based on impact.
Regards,
Shloke
Regards,
Shloke