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

Nya Valdez1
Tera Contributor

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.

find_real_file.png

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);
18 REPLIES 18

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.

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);

find_real_file.png

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

 

find_real_file.png

Output:

find_real_file.png

Hope this helps. Please mark the answer as correct/helpful based on impact.

Regards,
Shloke

Hope this helps. Please mark the answer as correct/helpful based on impact.

Regards,
Shloke

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!!!)

 

find_real_file.png

I've actually noticed that both the "Bcc" field AND the "To" field are being populated using the scripting changes that you've suggested (please see below) ... is there a way to completely avoid adding recipients to the "to" field altogether? Please advise.

find_real_file.png

 

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:

find_real_file.png 

My Notification Below:

find_real_file.png

Output:

find_real_file.png

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

Hope this helps. Please mark the answer as correct/helpful based on impact.

Regards,
Shloke